← GE HealthCare Interview Insights
I knew a stack was involved but spent too long trying to build it left-to-right before realizing scanning from the right makes way more sense.
Use a monotonic decreasing stack to efficiently compute visible people to the right. Iterate from right to left, maintaining a stack of indices with heights in decreasing order. For each person, pop shorter or equal heights, count the remaining stack elements (those visible), then push the current index.
Pro tip: Clarify the visibility condition: a person can see another if all between are strictly shorter than both. This means equal heights block visibility. Mentioning this shows attention to detail and avoids off-by-one errors.
Restate the visibility condition: person i can see person j (i < j) if all k between i and j have height < min(height[i], height[j]). Note that equal heights block visibility.
Use a monotonic decreasing stack to keep track of people to the right that are visible. The stack stores indices, and heights are strictly decreasing from bottom to top.
Process each person from rightmost to leftmost. For each person, pop from the stack while the stack is not empty and the height at the top is <= current height. The remaining stack size is the number of visible people to the right.
After counting, push the current index onto the stack. Store the count in an output array at the current index.
Time complexity is O(n) because each element is pushed and popped at most once. Space complexity is O(n) for the stack. Handle edge cases like empty array, single element, and all equal heights.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.