Dsa
Monotonic Stack
Easy
The Lookout Horizon
You are standing on top of a row of buildings. For each building, you look to the right and count how many buildings are strictly shorter than your current building. Your view is blocked as soon as you encounter a building that is the same height or taller. Output the 'visibility count' for each building.
Example:
Heights: [10, 6, 8, 5, 11, 9]
- Bldg 0 (10): Sees 6, 8, 5. Stopped by 11. Count: 3.
- Bldg 1 (6): Sees nothing (8 is taller). Count: 0.
- Bldg 2 (8): Sees 5. Stopped by 11. Count: 1.
- Bldg 3 (5): Sees nothing (11 is taller). Count: 0.
- Bldg 4 (11): Sees 9. End of row. Count: 1.
- Bldg 5 (9): End of row. Count: 0.
Result: 3 0 1 0 1 0
Key concepts
monotonic_stackvisibility
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now