Dsa
Monotonic Stack
Easy
The Security Guard Lineup
A line of N security guards stands at the entrance of a stadium, each with a specific height. For each guard, you need to identify the 'Next Taller Supervisor'. This is the 1-based index of the first guard to their right who is strictly taller than them. If no such guard exists, the value should be 0.
Example:
Heights: [10, 15, 12, 18, 11]
- Guard 1 (10): Next taller is Guard 2 (15). Output 2.
- Guard 2 (15): Next taller is Guard 4 (18). Output 4.
- Guard 3 (12): Next taller is Guard 4 (18). Output 4.
- Guard 4 (18): No one taller to the right. Output 0.
- Guard 5 (11): No one taller to the right. Output 0.
Result: 2 4 4 0 0
Key concepts
monotonic_stacknext_greater_element
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