FastPrepPrint Vertical Asterisk Bars

Print Vertical Asterisk Bars

Microsoft logoMicrosoft● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given an array of nonnegative bar heights, return a top-to-bottom text drawing of vertical bars.

Each output row contains exactly one character per input bar: * when that bar reaches the row and a space otherwise. Bars are bottom-aligned. Return an empty array when there are no positive heights.

Function

drawVerticalBars(heights: int[]) → String[]

Examples

Example 1

heights = [3,1,2]return = ["*  ","* *","***"]

The first bar reaches all three rows, the second reaches only the bottom row, and the third reaches the bottom two rows.

Example 2

heights = [0,0]return = []

No bar has positive height, so there are no rows to draw.

Constraints

  • 0 <= heights.length <= 1000
  • 0 <= heights[i] <= 1000

More Microsoft problems

See Microsoft hiring insights
public String[] drawVerticalBars(int[] heights) {
  // Write your code here.
}
heights[3,1,2]
expected["* ", "* *", "***"]
Checking account…