Intervals and Merge Problems
Six harder prompts on sorting intervals, sweep-line counts, overlap detection, and meeting-room scheduling. Code-anchored interview prep.
Question Bank
Hard
JavaScript
merge-intervals
sweep-line
algorithms
interview-prep
694 views
5
Implement merge(intervals) that returns the union of overlapping intervals. Each input interval is [start, end] (inclusive). Aim for O(n log n).
Examples
Example 1:
Input: intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]
Output: [[1, 6], [8, 10], [15, 18]]
Explanation: Sort by start. Walk with current = [1, 3]. Next [2, 6] overlaps so extend to [1, 6]. Next [8, 10] does not overlap, push current and reset. Same for [15, 18]. Sort dominates at O(n log n).5 more questions, with full solutions and explanations, are available for premium members.
Upgrade to Premium