Dsa
Monotonic Stack
Hard
Building Heights with View Counts
You are given an array of integers representing the heights of buildings in a row. Each building can see the sky unless blocked by a taller building in front of it. Your task is to find how many buildings each building can see to its right until a taller building is encountered.
Input format:
- A single line of space-separated integers representing building heights (1 <= heights[i] <= 10^4).
Output format:
- A single line of space-separated integers representing the number of visible buildings to the right for each building.
Example:
Input:
3 1 4 2 5
Output:
2 1 1 1 0
Key concepts
monotonic_stackgeometryalgorithm
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