← Questions

Buildings with an ocean view

0
MetaCodingLast asked Aug 2025

A coding interview question asked in Meta's technical phone screen, last asked Aug 2025. It tests arrays and monotonic stack.

Buildings stand in a row, and the ocean lies to the right of the last one. A building has an ocean view when it is taller than every building to its right. Return the indices of the buildings with a view, in increasing order.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • ocean_view(heights: list[int]) -> list[int] — a building of equal height to its right blocks the view.

Example

ocean_view([4, 2, 3, 1])   ->  [0, 2, 3]
ocean_view([4, 3, 2, 1])   ->  [0, 1, 2, 3]
ocean_view([1, 3, 2, 4])   ->  [3]
ocean_view([2, 2, 2])      ->  [2]          # equal heights block

Constraints

  • 1 <= len(heights) <= 10^5, 1 <= heights[i] <= 10^9.
  • O(n) time.

Clarifying questions

  • Does a building of equal height block the view?
  • Which side is the ocean on?
  • Should the output be in increasing order?
  • Is the input a fixed array, or do buildings keep arriving?

What a strong answer covers

  • A scan from the right that keeps the tallest height seen so far. A building has a view when it is taller than that maximum. Reverse the collected indices at the end.
  • O(n) time and O(1) extra space besides the output.
  • The left-to-right alternative with a monotonic stack, which is useful when buildings arrive one at a time from the left.
  • Tests: one building, all equal, strictly increasing, strictly decreasing.

Follow-ups

  • The ocean is on the left instead.
  • Equal heights do not block. What changes?
  • Buildings are added on the left one at a time (or on the right). Keep the answer current.
  • Oceans on both sides: return buildings that see either one.

Where it's asked

  • Meta · Technical phone screen · Last asked Aug 202545 min · Two coding problems in CoderPad, about 20 minutes each · Usually no AI; some newer screens are AI-assisted

What it tests

arrays · monotonic stack

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding