PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Find buildings with rightward view states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

  • Medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Find buildings with rightward view

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

You are given an array of building heights aligned along a street that faces the ocean to the right. Return the indices of buildings that can see the ocean (no strictly taller building to their right). Output indices in increasing order. Provide an O(n) solution and discuss variations for leftward views or non-strict comparisons.

Quick Answer: This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Find buildings with rightward view states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

You are given an integer array `heights` representing the heights of buildings aligned along a street that faces the ocean to the right. A building has an ocean view if there is no strictly taller building to its right. Return the list of indices (0-based) of all buildings that can see the ocean, in increasing order. Aim for an O(n) solution. Note that the rightmost building always has an ocean view, and a building blocks another only when it is *strictly* taller. Example: - Input: heights = [4, 2, 3, 1] - Output: [0, 2, 3] - Explanation: Building 3 (height 1) is rightmost so it sees the ocean. Building 2 (height 3) has only height 1 to its right, so it sees the ocean. Building 1 (height 2) is blocked by building 2 (height 3). Building 0 (height 4) is taller than everything to its right, so it sees the ocean.

Constraints

  • 1 <= heights.length <= 10^5 (the harness also accepts an empty array, returning [])
  • 1 <= heights[i] <= 10^9
  • Output indices must be in strictly increasing order

Examples

Input: ([4, 2, 3, 1],)

Expected Output: [0, 2, 3]

Explanation: Index 3 (rightmost) sees ocean. Index 2 (h=3) only has h=1 to its right. Index 1 (h=2) is blocked by index 2 (h=3). Index 0 (h=4) is taller than all to its right.

Input: ([4, 3, 2, 1],)

Expected Output: [0, 1, 2, 3]

Explanation: Strictly decreasing heights: every building is taller than all buildings to its right, so all see the ocean.

Input: ([1, 3, 2, 4],)

Expected Output: [3]

Explanation: Only the rightmost building (h=4) sees the ocean; every other building has a strictly taller building (height 4) somewhere to its right.

Input: ([2, 2, 2, 2],)

Expected Output: [3]

Explanation: Equal heights do not block (comparison is strict), but only the rightmost qualifies because each non-last building has an equal-height neighbor — equal is not strictly taller, yet the running-max starts at the rightmost, so indices 0-2 are not strictly greater than the max to their right (which equals 2). Only index 3 has nothing to its right.

Input: ([5],)

Expected Output: [0]

Explanation: Single building always sees the ocean.

Input: ([],)

Expected Output: []

Explanation: Empty input yields an empty result.

Input: ([1, 2, 3, 4, 5],)

Expected Output: [4]

Explanation: Strictly increasing: only the last (tallest, rightmost) building sees the ocean.

Hints

  1. Scan from right to left. A building sees the ocean iff it is taller than every building already seen to its right.
  2. Track the maximum height encountered so far while scanning right-to-left. A strictly taller building qualifies and updates that maximum.
  3. Because you scan right-to-left, collected indices come out in decreasing order — reverse them (or prepend) to return increasing order.
  4. For a LEFTWARD view, scan left-to-right with the same running-max logic. For NON-STRICT comparisons (>= blocks), change the comparison from `>` to `>` against the max but treat equal heights as blocking by comparing with `>=` from the blocker's perspective — i.e. keep a building only when it is strictly greater than the running max even under non-strict rules the running-max approach still applies, just adjust whether equal heights block.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)