1762. Buildings With an Ocean View
mediumAsked at MetaShort answer
- Best approach: Right-to-left sweep with running max. Walk from right to left. Track the max height seen so far. A building has ocean view iff its height > current max.
- Time O(n), space O(n) for output
- Verified
Given building heights, return indices that can see the ocean to the right. Meta asks this to test whether you reach for the right-to-left sweep that's O(n), not the O(n^2) per-building lookahead.
By Sam K., Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Meta loops.
- Glassdoor (2026-Q1)— Meta E3/E4 onsite reports cite this as a Meta-favorite warmup.
- Blind (2025-12)— Recurring Meta phone-screen problem.
Problem
There are n buildings in a line. You are given an integer array heights of size n that represents the heights of the buildings in the line. The ocean is to the right of the buildings. A building has an ocean view if the building can see the ocean without obstructions. Formally, a building has an ocean view if all the buildings to its right have a smaller height. Return a list of indices (0-indexed) of buildings that have an ocean view, sorted in increasing order.
Constraints
1 <= heights.length <= 10^51 <= heights[i] <= 10^9
Examples
Example 1
heights = [4,2,3,1][0,2,3]Example 2
heights = [4,3,2,1][0,1,2,3]Example 3
heights = [1,3,2,4][3]Approaches
1. Brute-force lookahead per index
For each i, check whether all heights[j] for j > i are strictly less than heights[i].
- Time
- O(n^2)
- Space
- O(1)
function findBuildingsBrute(heights) {
const result = [];
for (let i = 0; i < heights.length; i++) {
let canSee = true;
for (let j = i + 1; j < heights.length; j++) {
if (heights[j] >= heights[i]) { canSee = false; break; }
}
if (canSee) result.push(i);
}
return result;
}Tradeoff: Quadratic — fails at the upper bound. Mention only to anchor the optimal.
2. Right-to-left sweep with running max (optimal)
Walk from right to left. Track the max height seen so far. A building has ocean view iff its height > current max.
- Time
- O(n)
- Space
- O(n) for output
function findBuildings(heights) {
const result = [];
let maxRight = 0;
for (let i = heights.length - 1; i >= 0; i--) {
if (heights[i] > maxRight) {
result.push(i);
maxRight = heights[i];
}
}
return result.reverse();
}Tradeoff: Linear time. The right-to-left direction is the key — every building only needs to compare against the max of everything to its right, which we accumulate in one variable.
Meta-specific tips
Meta interviewers grade this on whether you recognize the right-to-left direction. State out loud: 'I'm walking right-to-left because every building's ocean view depends only on what's to its right.' This problem is a Meta favorite specifically because the wrong-direction sweep doesn't work — testing whether you can identify the natural processing direction.
Common mistakes
- Sweeping left-to-right, then needing extra storage for the suffix max.
- Forgetting to reverse the result at the end (we pushed indices in reverse order).
- Using >= instead of > — the problem says strictly smaller, so equal heights block the view.
Follow-up questions
An interviewer at Meta may pivot to one of these next:
- What if the ocean were on both sides?
- What if buildings could be in 2D (towers in a city)?
- What if heights could change online (insertions)?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why right-to-left and not left-to-right?
Each building's ocean view depends only on the max of the buildings TO ITS RIGHT. Right-to-left lets us accumulate that max in O(1) extra space.
Could we do it left-to-right?
Yes — precompute a suffix max array (O(n) space), then sweep left-to-right. Same time complexity but more space. Right-to-left wins on elegance.
Originally published on InterviewChamp.AI