Find Buildings With an Unobstructed View to the Ocean
Last updated: November 29, 2026
Quick Overview
Buildings stand in a row with the ocean to the right. A building has a view if every building to its right is strictly shorter. Return the indices of buildings with a view in increasing order.
Meta
November 29, 20260
0
0 solved
Buildings stand in a row with the ocean to the right. A building has a view if every building to its right is strictly shorter. Return the indices of buildings with a view in increasing order.
This is a well known Meta screen problem that rewards seeing the right-to-left scan immediately. Interviewers also ask the left-to-right streaming variant to see if you can reach for a monotonic stack.
What the Interviewer Expects
- Scans from the right while tracking the tallest seen so far
- Uses a strict comparison so equal heights block the view
- Reverses the collected indices for ascending output
- Offers the monotonic stack version for left-to-right input
- States O(n) time and O(1) extra space
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Heights arrive left to right as a stream; maintain the answer online.
- The ocean is on both sides; return buildings that see either side.
- Return, for each building, the index of the next taller building to its right.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list of positive heights. Output ascending indices. A building with an equal-height building to its right does not have a view. O(n) time....
High-Level Design
Walk from the last building to the first, keeping tallest, the maximum height seen to the right. A building has a view exactly when it is strictly taller than tallest; then it becomes the new `tal...