Merge Overlapping Intervals
Company: Atlassian
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: Merge overlapping closed intervals with a sorted scan, preserving nested coverage, handling touching endpoints, and explaining the union invariant and complexity.
Constraints
- The list contains 0 through 2^31 - 1 intervals.
- Every interval is exactly [start, end] with start <= end.
- Both endpoints are integers from -(2^53 - 1) to 2^53 - 1, inclusive.
Examples
Input: ([[9, 12], [2, 6], [1, 4], [15, 17]],)
Expected Output: [[1, 6], [9, 12], [15, 17]]
Explanation: The first two intervals after ordering by start overlap. Two later gaps remain.
Input: ([[4, 8], [1, 4], [8, 8]],)
Expected Output: [[1, 8]]
Explanation: The intervals meet at endpoints, so their union is a single closed interval.
Hints
- When checking whether two closed intervals overlap, include both endpoints.