Merge overlapping intervals
Company: OpenAI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates algorithmic problem-solving skills focused on interval manipulation, sorting and merging logic, and time/space complexity analysis within the Coding & Algorithms domain, emphasizing practical application.
Constraints
- 0 <= len(intervals) <= 10000
- intervals[i].length == 2
- -10000 <= start <= end <= 10000
Examples
Input: [[1,3],[2,6],[8,10],[15,18]]
Expected Output: [[1,6],[8,10],[15,18]]
Explanation: [1,3] and [2,6] overlap, so they merge into [1,6]. The other intervals do not overlap.
Input: [[1,4],[4,5]]
Expected Output: [[1,5]]
Explanation: The intervals touch at 4, so they are considered overlapping and merge into [1,5].
Hints
- Sorting the intervals by their start value makes it easier to detect overlaps in one pass.
- Keep track of the current merged interval and extend its end when the next interval overlaps.