Google questions

Visible People in Line

GooglePhone screenMedium

Line(heights) holds people standing in a line, front to back, where heights[i] is the height of person i. Person i and person j can see each other when every person standing between them is at most as tall as the taller of the two; neighbors can always see each other.

Part 1: What the last person sees

Implement last_sees(), which returns the number of other people the last person in the line can see. The last person stands at index len(heights) - 1 and only looks toward the front of the line.

  1. Example 1
    init
    [[1,10,6,7,9,8,2,4,3,5]]
    operations
    [["last_sees",[]]]
    Output
    [6]

    Why: The last person, height 5, sees the people of heights 3, 4, 2, 8, 9 and 10. The 7 and the 6 are blocked by the 9 standing between them and the end of the line, and the 1 is blocked by the 10.

Constraints

  • 1 <= len(heights) <= 10^5
  • 1 <= heights[i] <= 10^9
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Visible People in Line: Google interview question | LoopCoach