Compute city skyline outline
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's understanding of interval processing and skyline/contour computation within computational geometry, focusing on reasoning about overlapping ranges and height aggregation.
Constraints
- 1 <= n <= 10^4
- 0 <= L < R <= 10^9
- 1 <= H <= 10^9
- Each building is a triple [L, R, H] of integers.
- Output must be sorted by x, with no two consecutive equal heights, first height > 0, last height = 0.
Examples
Input: (3, [[2, 9, 10], [3, 7, 15], [5, 12, 12]])
Expected Output: [[2, 10], [3, 15], [7, 12], [12, 0]]
Explanation: The prompt example: height 10 from x=2, rises to 15 at x=3, drops to 12 at x=7 once the tallest ends, then to 0 at x=12.
Input: (1, [[0, 2, 3]])
Expected Output: [[0, 3], [2, 0]]
Explanation: A single building: height 3 from x=0 to x=2, then ground.
Input: (2, [[0, 5, 3], [0, 5, 3]])
Expected Output: [[0, 3], [5, 0]]
Explanation: Two identical overlapping buildings form one rectangle; only one rise and one fall remain.
Input: (2, [[1, 3, 5], [3, 6, 5]])
Expected Output: [[1, 5], [6, 0]]
Explanation: Edge-touching buildings of equal height merge into a flat top from x=1 to x=6; no spurious point at the shared x=3.
Input: (2, [[2, 4, 7], [2, 4, 5]])
Expected Output: [[2, 7], [4, 0]]
Explanation: The shorter building is fully hidden behind the taller one over the same span, so it never appears in the outline.
Input: (2, [[0, 10, 5], [3, 6, 8]])
Expected Output: [[0, 5], [3, 8], [6, 5], [10, 0]]
Explanation: A taller building nested inside a wider one creates a bump: 5, up to 8, back to 5, then 0.
Input: (3, [[1, 5, 6], [2, 8, 6], [10, 12, 4]])
Expected Output: [[1, 6], [8, 0], [10, 4], [12, 0]]
Explanation: Two overlapping equal-height buildings form one block ending at x=8 (drop to 0), then a disjoint building gives a separate segment from x=10 to x=12.
Hints
- Think of each building as two events: a 'start' at x = L (height rises) and an 'end' at x = R (height may fall). Sort all events by x.
- At any x, the skyline height equals the tallest building currently spanning that x. A max-heap of active building heights gives this in O(log n).
- Use lazy deletion: instead of removing a building's height when it ends, store its right edge with it in the heap and pop entries whose end <= current x before reading the top.
- Emit a key point only when the current max height differs from the last emitted height. When several events share the same x, collapse them so only the final height at that x is recorded.