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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

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
Arrays
Monotonic stack
Greedy scan
Streaming
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported