The Buildings With an Ocean View interview question asks you to identify which buildings in a row have an unobstructed view of the ocean. You are given an array heights where heights[i] is the height of the i-th building. The ocean is to the right of the last building. A building has an ocean view if all the buildings to its right are strictly shorter than it. The goal is to return a list of indices of buildings that have an ocean view, sorted in increasing order. This Buildings With an Ocean View coding problem is a classic example of "look-ahead" processing.
Tech companies like Meta and Google use this question to test a candidate's ability to efficiently process linear data. It evaluates whether you can avoid a naive O(N^2) solution (checking every building to the right for every building) in favor of a more optimal O(N) approach. It also tests your familiarity with monotonic stacks or simple backward iteration.
This problem follows the Array, Monotonic Stack, Stack interview pattern. There are two main ways to solve it:
Suppose heights = [4, 2, 3, 1].
Whenever a problem involves "elements to the right that are larger/smaller," think of Monotonic Stacks or Backward Iteration. These are the most common patterns for optimizing range-based comparisons in linear time.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Sum of Subarray Ranges | Medium | Solve | |
| Next Greater Element II | Medium | Solve | |
| Count Bowl Subarrays | Medium | Solve | |
| Maximal Range That Each Element Is Maximum in It | Medium | Solve | |
| Maximum of Minimum Values in All Subarrays | Medium | Solve |