cswork / libraryOpen practice workspace

Coding questions · Meta

Find buildings with rightward view

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 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.

What a Strong Answer Covers

  • A clear algorithm with the right data structures and enough pseudocode or code-level detail to implement it.
  • A correctness argument that explains why the algorithm covers all required cases.
  • Time and space complexity, plus at least one alternative approach when relevant.
  • Focused tests for normal cases, edge cases, and failure modes.

Follow-up Questions

  • How would the approach change if the input were streaming or too large for memory?
  • What invariants would you assert in production code?
  • Which tests would catch off-by-one, duplicate, or tie-breaking bugs?

Find Buildings With Rightward Ocean View

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

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.

Solution

Solution Alignment

The prompt asks for an implementation-level answer. The safest way to present it is to define the state, maintain clear invariants, then walk through complexity and tests.

Problem Restatement

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.

Recommended Approach

Choose traversal based on the required output. DFS is natural for subtree computations, reconstruction, and range pruning; BFS is natural for level order or side views. Keep per-depth or per-position state when the output depends on columns, rows, or depths.

Correctness

The implementation should maintain an invariant after each loop or operation that directly matches the problem statement. At termination, that invariant implies the returned value has considered every valid candidate exactly once, or has preserved the required data-structure state after every API call.

Complexity

Most tree traversals are O(n) time and O(h) recursion stack for DFS or O(w) queue space for BFS, where h is height and w is maximum width.

Edge Cases and Tests

Empty tree, one node, skewed tree, duplicate values when reconstruction assumes uniqueness, deep recursion, and tie-breaking for same row/column nodes.