Count Visible People to the Left
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Google OA, reported in October 2026, is writing the obvious double loop and calling it done. You get an array of distinct heights, and for each person you count how many people to the left they can see. It's a monotonic stack problem wearing a visibility costume. If you brute force it, n squared dies on big inputs. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but you should know the shape before you start.
The problem
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]). 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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Here's the trick. Person j is visible from i when nothing between them is taller than both. Walk left to right and keep a stack of heights that is strictly decreasing. For person i, pop every height smaller than heights[i]. Each popped person is visible, so count them. Then if the stack is still non-empty, the top is taller and also visible, so add one. Push i. Check it against the example: for the last height 5, you pop 3, 4, 2 and 8's neighbors in the chain, ending with 6 visible. The pitfall is forgetting that extra +1 for the taller blocker, or assuming visibility stops at the first taller person without counting that person. Distinct heights mean you skip tie handling. Each index is pushed and popped once, so it runs in O(n). If the stack logic slips under pressure, StealthCoder can hand you the solution live, but trace the sample by hand first.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Count Visible People to the Left cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Visible People to the Left FAQ
What's the trick for Count Visible People to the Left?+
Use a monotonic decreasing stack. For each person, pop everyone shorter and count those pops, since they're all visible. If anyone remains on the stack, that taller person is also visible, so add one. Then push the current person. Total work is linear.
How hard is this one really?+
Medium. The idea is short once you know monotonic stacks, but the off-by-one on the taller blocker trips people. If you've never written a decreasing stack, the brute force feels natural and wastes your time.
Why does the double loop fail?+
Checking every pair with a max scan in between is O(n squared) or even O(n cubed). Large inputs will time out. The stack collapses it to O(n) because each person is pushed once and popped once.
Do I need to handle equal heights?+
No. The problem says all heights are distinct, so you don't need tie rules. Strict comparisons are enough, and you can pop on any smaller height without worrying about equals.
How do I prepare in 48 hours?+
Write the decreasing stack solution from scratch twice. Trace the example [1,10,6,7,9,8,2,4,3,5] and confirm you get [0,1,1,2,3,2,3,4,4,6]. Then try a variant counting visible people to the right. That covers the pattern Google reported in October 2026.