Google VO Interview Question: Visible People

20 Views
No Comments

Input: [1 10 6 7 9 8 2 4 3 5]

Output: How many people can the last one in line see?

Clarification:

  • A person A can see another person B in front of them if there is no one taller than both A and B standing between them.
  • Every person has a different height.

This problem asks for the number of people visible to the last person in line. A person is visible only if no one taller than both people stands between them. Since all heights are distinct, the solution can scan the array from right to left while maintaining the tallest height seen so far. Each time a taller person is found, they are visible and the maximum is updated. This is a simple linear-time array scan with O(n) time complexity.

END
 0