Merge overlapping intervals
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates understanding of interval operations, array manipulation, and algorithmic efficiency, along with correct handling of edge cases and large-scale inputs.
Constraints
- Intervals are [start, end] pairs with start <= end.
- Closed intervals that touch at an endpoint are considered overlapping.
- Return intervals sorted by start for deterministic grading.
Examples
Input: ([[1,3],[2,6],[8,10],[15,18]],)
Expected Output: [[1, 6], [8, 10], [15, 18]]
Explanation: The first two intervals overlap and merge.
Input: ([[1,4],[4,5]],)
Expected Output: [[1, 5]]
Explanation: Closed intervals that touch are merged.
Input: ([[5,7],[1,2],[2,3],[10,10]],)
Expected Output: [[1, 3], [5, 7], [10, 10]]
Explanation: Input order does not matter.
Input: ([],)
Expected Output: []
Explanation: An empty interval list returns empty output.
Hints
- Sort by start first.
- Extend the previous merged interval whenever the next interval overlaps or touches it.