Airbnb questions

Terrain Rendering

AirbnbPhone screenMedium

You are drawing a side view of terrain made of columns. heights[i] is the height of column i, a whole number of cells.

Write render_terrain(heights), which returns the picture as a list of text lines, from the top line to the bottom one. The picture has one character per column per line. A column of height h is + in its lowest h lines above the base, and a blank everywhere else. The last line is the base: one + for every column. The first line is the line of the tallest column, so a picture has max(heights) lines above the base. Lines have no trailing spaces, but spaces on the left are kept. If every height is 0, the picture is only the base line.

Part 1: Draw the terrain

For heights = [5, 4, 3, 2, 1, 3, 4, 0, 3, 4] the result has six lines: five rows above the base, the tallest column making the first line a single +, and then the base.

  1. Example 1
    heights
    [5,4,3,2,1,3,4,0,3,4]
    Output
    ["+","++    +  +","+++  ++ ++","++++ ++ ++","+++++++ ++","++++++++++"]

    Why: Each column is + for as many rows as its height, and the last line is the base. Picture: ``` + ++ + + +++ ++ ++ ++++ ++ ++ +++++++ ++ ++++++++++ ```

Constraints

  • 1 <= len(heights) <= 100
  • 0 <= heights[i] <= 50
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Terrain Rendering: Airbnb interview question | LoopCoach