The Skyline Problem

Compute the skyline of given rectangular buildings in a 2D city, eliminating hidden lines and forming the outer contour of the silhouette when viewed from a distance. Each building is described by its geometric information in the form of an array BUILDINGS[i] = [LEFT_i, RIGHT_i, HEIGHT_i].

Explanation:

  • LEFT_i: x-coordinate of the left edge of the i-th building.
  • RIGHT_i: x-coordinate of the right edge of the i-th building.
  • HEIGHT_i: height of the i-th building.

It is assumed that all buildings are perfect rectangles grounded at height 0.

The skyline must be represented as a list of "key points" sorted by x-coordinate in the format [[x1, y1], [x2, y2], ...]. Each key point marks the left endpoint of a horizontal segment in the skyline, except for the last point, which always has y-coordinate 0 to indicate the skyline's termination.

Input:

The first line contains an integer N indicating the number of buildings.
Each of the following N lines contains three space-separated integers LEFT_i, RIGHT_i, and HEIGHT_i for each building.

Output:

Return a list of key points representing the skyline.
The format is [[x1, y1], [x2, y2], ...], with each point marking a key horizontal segment.

Example:

Input:
N = 2
BUILDINGS = [[2, 9, 10], [3, 7, 15]]

Output:
[[2, 10], [3, 15], [7, 10], [9, 0]]

Constraints:

  • 1 <= |BUILDINGS| <= 10^4
  • 0 <= LEFT_i < RIGHT_i <= 2^31 - 1
  • 1 <= HEIGHT_i <= 2^31 - 1
  • Time Limit: 1 sec

Note:

Ensure no consecutive horizontal segments of equal height in the output. If any, merge them into one.
AnswerBot
4mo

Compute the skyline of given rectangular buildings in a 2D city, eliminating hidden lines and forming the outer contour of the silhouette.

  • Iterate through the buildings and create a list of critical po...read more

Help your peers!
Select
Add answer anonymously...

Samsung Research Software Developer interview questions & answers

A Software Developer was asked 12mo agoQ. Design and implement a data structure for Least Recently Used (LRU) cache. It sh...read more
A Software Developer was asked 12mo agoQ. Trie Implementation
A Software Developer was asked Q. The Skyline Problem Compute the skyline of given rectangular buildings in a 2D c...read more

Popular interview questions of Software Developer

A Software Developer was asked 12mo agoQ1. Design and implement a data structure for Least Recently Used (LRU) cache. It sh...read more
A Software Developer was asked 4d agoQ2. What is the number of ways to reach the bottom right corner from the top left co...read more
A Software Developer was asked 4d agoQ3. What is the definition of Object-Oriented Programming (OOP), and how does polymo...read more
Samsung Research Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits