MEDIUMasked at 2 companies

Buildings With an Ocean View

A medium-tier problem at 81% community acceptance, tagged with Array, Stack, Monotonic Stack. Reported in interviews at Meta and 1 others.

Founder's read

Buildings With an Ocean View is a medium-difficulty array problem that appears in Meta and Coupang interviews. You're given building heights and need to return which ones can see the ocean, always assuming the ocean is to the right. The naive approach is O(n^2), but candidates who don't spot the monotonic stack pattern will timeout or waste minutes on optimization. If this problem hits your live OA and you blank on the trick, StealthCoder solves it in seconds, invisible to the proctor. With an 81% acceptance rate, most people who've drilled it pass, but unprepared candidates hit a wall.

Companies asking
2
Difficulty
MEDIUM
Acceptance
81%

Companies that ask "Buildings With an Ocean View"

If this hits your live OA

Buildings With an Ocean View is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share.

Get StealthCoder
What this means

The key insight is that a building sees the ocean if no taller building exists to its right. Iterating left-to-right and checking each building against all rightward neighbors is O(n^2) and fails on large inputs. The trick is to reverse-iterate from right to left, maintaining a monotonic stack of building heights. As you go backward, you pop shorter buildings from the stack and track the maximum. Any building taller than the stack top can see the ocean. This reduces the problem to O(n) time and O(n) space. Most candidates either miss the reversal entirely or build a non-monotonic structure. The monotonic stack is the pattern you need to internalize. StealthCoder is the hedge if the reversal and monotonic logic don't click during the timed assessment.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Buildings With an Ocean View recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Buildings With an Ocean View interview FAQ

Is this problem actually hard or is 81% acceptance misleading?+

The high acceptance reflects that many candidates have memorized monotonic stack patterns. But if you haven't drilled stack-based problems, the reversal and why you iterate backward is not obvious. You'll see O(n^2) solutions submitted that pass small cases but TLE on large ones. The problem isn't conceptually hard once you see it, but the path to seeing it is steep.

Do Meta and Coupang really ask this the same way?+

Both companies report this problem in their interview feedback. The core algorithmic challenge is identical across platforms. Variant framings might change (some ask for indices, some heights), but the monotonic stack pattern is what they're testing. Check their specific problem statement on their assessment platform for exact I/O format.

What's the relationship between monotonic stack and this problem?+

A monotonic stack maintains elements in order (increasing or decreasing) by popping elements that violate the order. Here, you maintain decreasing heights from right to left. When a taller building arrives, it 'blocks' all shorter buildings to its left from seeing the ocean. This is a canonical monotonic stack use case.

How do I avoid the O(n^2) trap?+

Don't think left-to-right with a rightward scan. That's the intuitive but wrong direction. Force yourself to iterate backward and ask: what information from the right do I need to know about each building. Maintaining a max or monotonic structure right-to-left is the pattern.

Should I memorize this solution or understand the monotonic stack?+

Understand the monotonic stack. This problem is a specific instance of that broader pattern. Once you get why iteration direction matters and why you maintain an ordered structure, you can apply it to similar problems like daily temperatures or next greater element.

Want the actual problem statement? View "Buildings With an Ocean View" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.