Count Visible People to the Left
Updated
Problem statement
There are n people standing in a line, indexed from left to right. Every person faces left, and heights[i] is person i's height. All heights are distinct.
Person i can see person j to the left, where j < i, exactly when no person k between them is taller than both endpoints. In other words, there must be no k with j < k < i and heights[k] > max(heights[i], heights[j]).
Return an integer array answer of length n, where answer[i] is the number of people that person i can see to the left.
Function
countVisibleToLeft(heights: int[]) → int[]Examples
Example 1
heights = [1,10,6,7,9,8,2,4,3,5]return = [0,1,1,2,3,2,3,4,4,6]The warm-up asks only about the last person, whose height is 5. Looking left, that person can see heights 3, 4, 2, 8, 9, and 10, so the last count is 6. Height 9 blocks heights 7 and 6, while height 10 blocks height 1. Applying the same rule to every position gives the returned array.
Example 2
heights = [5]return = [0]The only person has nobody to the left.
Example 3
heights = [5,1,4,2,3]return = [0,1,2,2,3]The final person sees heights 2, 4, and 5. Height 4 blocks height 1 because it is taller than both 1 and 3.
Constraints
1 <= heights.length <= 200000.1 <= heights[i] <= 10^9.- All values in
heightsare distinct. - Your solution must run in
O(n)time.
More Google Onsite Interview questions
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Trapping Rain WaterTeradata · ONSITE INTERVIEW
- Trapping Rain WaterBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Construct a Tree from Level-Order and Inorder TraversalsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Largest Rectangle in HistogramTekion · ONSITE INTERVIEWTekion interview questions
- Trapping Rain WaterAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Trapping Rain WaterKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Confirmed Profiling Events with First-Observed StartsAnthropic · PHONE SCREEN · ONSITE INTERVIEWAnthropic interview questions
- Trapping Rain WaterNuro · ONSITE INTERVIEWNuro interview questions
- Largest Rectangle in HistogramAirwallex · PHONE SCREENAirwallex interview questions
- Trapping Rain WaterMicrosoft · ONSITE INTERVIEWMicrosoft interview questions