Magicsheet logo

The Skyline Problem

Hard
37.1%
Updated 6/1/2025

The Skyline Problem

What is this problem about?

The Skyline Problem is a classic computational geometry challenge. You are given the locations and heights of several rectangular buildings in a city. Since the buildings are rectangles and share the same bottom (the ground), the city's silhouette is a collection of horizontal and vertical segments. Your task is to find the key "critical points" that define this skyline—essentially the points where the height of the tallest building at that location changes.

Why is this asked in interviews?

This the Skyline Problem interview question is a legendary hard problem asked by Apple, Google, Amazon, and Meta. it tests your ability to handle complex geometric data and maintain a dynamic state (the current maximum height) as you "sweep" across the x-axis. It evaluates your choice of data structures, such as priority queues or segment trees, and your ability to handle tricky edge cases like overlapping buildings or buildings of the same height.

Algorithmic pattern used

The problem utilizes the Array, Divide and Conquer, Line Sweep, Ordered Set, Heap (Priority Queue), Binary Indexed Tree, Segment Tree interview pattern. The most common approach is the "Line Sweep":

  1. Represent each building as two events: a "start" at x-coordinate with height H and an "end" at x-coordinate with height H.
  2. Sort all events by x-coordinate. For same x, handle starts before ends, and sort heights to avoid redundant points.
  3. Use a max-heap to store the heights of all buildings currently "active" at the current x.
  4. As you sweep from left to right:
    • If it's a "start" event, add the height to the heap.
    • If it's an "end" event, remove the height from the heap.
    • If the maximum height in the heap changes, the current (x, max_height) is a critical point of the skyline.

Example explanation

Building A: [2, 9, 10] (x_start 2, x_end 9, height 10). Building B: [3, 7, 15] (x_start 3, x_end 7, height 15). Events: (2, start 10), (3, start 15), (7, end 15), (9, end 10).

  1. At x=2: Add 10. Max is 10. Point: (2, 10).
  2. At x=3: Add 15. Max is 15. Point: (3, 15).
  3. At x=7: Remove 15. Max is now 10. Point: (7, 10).
  4. At x=9: Remove 10. Max is 0. Point: (9, 0). The skyline is [(2, 10), (3, 15), (7, 10), (9, 0)].

Common mistakes candidates make

In "The Skyline Problem coding problem," a major pitfall is incorrectly sorting the events at the same x-coordinate. If not handled carefully, you might generate extra points with the same x or zero height. Another common issue is the inefficiency of removing an arbitrary element from a standard heap (O(n)). This can be solved by using a "lazy removal" strategy or a more advanced data structure like a balanced BST or a Segment Tree.

Interview preparation tip

Line sweep is a vital technique for geometry and interval problems. Practice it by identifying "events" and choosing the right data structure to maintain the "current state" of the sweep. For the skyline problem, ensure you have a robust way to handle multiple buildings starting or ending at the same location.

Similar Questions