List Buildings That Clear Every Building to Their Right

Quick Overview

List Buildings That Clear Every Building to Their Right. Buildings have heights heights, and the ocean lies to their right. A building sees the ocean exactly when every building to its right is strictly shorter.

List Buildings That Clear Every Building to Their Right

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Buildings have heights `heights`, and the ocean lies to their right. A building sees the ocean exactly when every building to its right is strictly shorter. Return the zero-based indices of all such buildings in increasing order. ```cpp class Solution { public: vector<int> findBuildings(vector<int>& heights); }; ``` ### Examples **Example 1:** ```text heights = [6,2,5,5,1] Output: [0,3,4] Explanation: Equal height at index 3 blocks index 2. ``` **Example 2:** ```text heights = [2,2,2] Output: [2] Explanation: Only the rightmost building has no equal-height obstruction. ``` ### Constraints - `1 <= heights.length <= 10^5`. - Heights are integers in `[1,10^9]`. ### Clarifying Questions Equal height blocks the view. Output indices must be increasing, even if the scan proceeds from right to left. ```hint Inspect the structure How do equal-height buildings affect the visibility rule and the required output order? ``` ### Reasoning checkpoints - Apply strict right-side obstruction, including equal-height buildings. - Return all and only the qualifying original indices in increasing order. ### Optional extensions How would the answer change if equal-height buildings did not obstruct one another?

Overview: List Buildings That Clear Every Building to Their Right. Buildings have heights heights, and the ocean lies to their right. A building sees the ocean exactly when every building to its right is strictly shorter.

Meta logo
Meta
Jun 1, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Buildings have heights heights, and the ocean lies to their right. A building sees the ocean exactly when every building to its right is strictly shorter. Return the zero-based indices of all such buildings in increasing order.

class Solution {
public:
    vector<int> findBuildings(vector<int>& heights);
};

Examples

Example 1:

heights = [6,2,5,5,1]
Output: [0,3,4]
Explanation: Equal height at index 3 blocks index 2.

Example 2:

heights = [2,2,2]
Output: [2]
Explanation: Only the rightmost building has no equal-height obstruction.

Constraints

  • 1 <= heights.length <= 10^5 .
  • Heights are integers in [1,10^9] .

Clarifying Questions Guidance

Equal height blocks the view. Output indices must be increasing, even if the scan proceeds from right to left.

Reasoning checkpoints

  • Apply strict right-side obstruction, including equal-height buildings.
  • Return all and only the qualifying original indices in increasing order.

Optional extensions

How would the answer change if equal-height buildings did not obstruct one another?