Buildings With an Ocean View gives you an array heights of building heights and asks for the buildings with an unobstructed ocean view. The original LeetCode 1762 puts the ocean on one side only — the right — but a common interviewer follow-up flips it into a two-sided version: the buildings sit on a strip of land with water on both sides, and a building only has a view if it's unobstructed left and right at the same time.
The original one-sided trick, and why it doesn't just double
In the one-sided version, you scan from the ocean side inward, tracking a single running max, and every building taller than that running max gets a view. It's tempting to assume the two-sided version is just "do that twice" — scan left-to-right, scan right-to-left, combine the results. That instinct is half right: you do need both directions, but the two results don't combine with OR.
The rule: unobstructed on both sides at once
A building counts here only if it's strictly taller than every building to its left, and strictly taller than every building to its right — both conditions, not either one:
leftMax[i] = max(heights[0..i-1]) // -infinity if i == 0
rightMax[i] = max(heights[i+1..n-1]) // -infinity if i == n-1
hasView(i) = heights[i] > leftMax[i] AND heights[i] > rightMax[i]
Building leftMax and rightMax is a direct generalization of the one-sided technique — one running max scanned left-to-right, one scanned right-to-left, each stored into an array as you go. O(n) time, O(n) space, and it's the answer most people write first.
The insight hiding in the generalization
Here's what makes this a good follow-up: leftMax[i] and rightMax[i] together cover every other building in the array — nothing is left out. So heights[i] > leftMax[i] AND heights[i] > rightMax[i] isn't really two separate conditions, it's one: heights[i] is strictly greater than every other building in the entire array.
That means only the single tallest building in the whole array can ever qualify — and only if that height is unique. If the tallest height appears more than once, no building qualifies, because neither of the tied-tallest buildings is strictly taller than the other, so each blocks the other's claim.
The actual O(n), O(1) solution
Once you see that reduction, the two prefix/suffix arrays aren't needed at all. A single pass is enough: track the maximum height seen and how many times it occurs.
maxHeight = -infinity
maxIndex = -1
countAtMax = 0
for i from 0 to n-1:
if heights[i] > maxHeight:
maxHeight = heights[i]
maxIndex = i
countAtMax = 1
else if heights[i] == maxHeight:
countAtMax += 1
return [maxIndex] if countAtMax == 1 else []
No arrays, no second pass — O(1) extra space instead of O(n), and a much smaller result than most people expect walking in, since the "obvious" answer set from the one-sided problem (often many buildings) collapses down to at most one.
Complexity
O(n) time either way, but the two-array approach carries O(n) extra space to compute something that provably resolves to a single index or an empty result. That gap — between "the generalization that works" and "the generalization that's actually efficient" — is exactly the point of asking this as a follow-up rather than a standalone problem.
The follow-up interviewers ask
The natural next question flips the AND back to OR: a building has a view if it's unobstructed on either side, not both. That's the version where the naive two-pass instinct is actually correct — compute the one-sided ocean-view set scanning from the left, compute it again scanning from the right, and the answer is the union of the two, which can include most of the array rather than at most one building. Comparing the two variants side by side is usually what reveals whether a candidate actually understood the AND-collapses-to-a-single-max insight, or just landed on the right answer for the wrong reason.