Compute city skyline silhouette
Company: TikTok
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: Compute city skyline silhouette evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Constraints
- 0 <= n (n is the number of buildings; n may be 0)
- Each building is [L, R, H] with 0 <= L < R and H > 0
- Coordinates and heights may be large; the algorithm only compares values, so very large coordinates are fine
- Return key points sorted by x ascending with strictly alternating heights, ending at height 0
Examples
Input: [[2,9,10],[3,7,15],[5,12,12],[15,20,10],[19,24,8]]
Expected Output: [[2, 10], [3, 15], [7, 12], [12, 0], [15, 10], [20, 8], [24, 0]]
Explanation: Canonical LC218 example. The silhouette rises as taller buildings begin, drops to 0 in the gap between x=12 and x=15, then resumes for the second cluster.
Input: [[0,2,3],[2,5,3]]
Expected Output: [[0, 3], [5, 0]]
Explanation: Two equal-height buildings touching at x=2 (Ri == Lj). The shared border produces no spurious key point — height stays 3 until the right end.
Input: [[1,2,1],[1,2,2],[1,2,3]]
Expected Output: [[1, 3], [2, 0]]
Explanation: Three identical-span buildings of heights 1,2,3. Only the tallest is visible, giving a single rise to 3 and a drop to 0.
Input: [[1,5,4],[2,4,4]]
Expected Output: [[1, 4], [5, 0]]
Explanation: A fully nested building of the same height as its container contributes nothing to the silhouette.
Input: [[0,5,7],[1,3,9],[2,4,9]]
Expected Output: [[0, 7], [1, 9], [4, 7], [5, 0]]
Explanation: Two equal-height taller buildings overlap on top of a lower base; height stays 9 across the union [1,4) then falls back to the base 7.
Input: []
Expected Output: []
Explanation: Empty input yields an empty skyline.
Input: [[3,10,8]]
Expected Output: [[3, 8], [10, 0]]
Explanation: A single building gives exactly two key points: the rise at its left edge and the drop to 0 at its right edge.
Input: [[2,9,10],[9,12,10]]
Expected Output: [[2, 10], [12, 0]]
Explanation: Adjacent same-height buildings sharing the border x=9 form one continuous plateau; no key point is emitted at x=9.
Hints
- Convert each building into two sweep-line events at its left and right edge, then sweep x from left to right while tracking the current maximum height among 'live' buildings.
- Use a max-heap keyed by height. The hard part is removal: a binary heap can't pop an arbitrary element, so use lazy deletion — store each building's right edge alongside its height and discard heap-top entries whose right edge has already been passed.
- Handle ties at a shared x carefully: process all events at the same x together, and emit a key point only when the resulting max height differs from the last emitted height. Encoding start edges as (x, -H, R) and end edges as (x, 0, 0) makes the sort order do the tie-breaking for you (starts before ends; taller starts first).