Skip to main content
All Questions

Count how many people in an array of heights are visible to the last person.

Unlock detailed company stats for this questionUpgrade

Given an array representing the heights of people, determine how many people are visible to the last person in the array.

Rule: person A is visible to person B if there is no one between them who is taller than both A and B.

Example: input [1, 10, 5, 7, 6, 3, 2, 4] gives output 5, since the people with heights 2, 3, 6, 7 and 10 are visible to 4.

Follow-up: find the number of visible people for every person in the list, not just the last one.

Goal: an optimized O(n) time solution for both cases.

Expect follow-ups like

  • Find the number of visible people for every person in the list, in O(n).

Community answers

No answers contributed by the community yet.

Related courses

Course

Solutions Architect Interview Prep

Land your dream solutions architect job at Amazon, Google, Meta, Microsoft, Salesforce, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates. Practice system design principles and leadership skills to ace your interviews.

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.