Overview
Interval problems deal with ranges [start, end]: meetings, bookings, time slots, number ranges. Almost all of them start the same way: sort by start time (or end time), then scan once while comparing each interval with the previous one or with a running boundary.
Two intervals [a, b] and [c, d] overlap when a <= d and c <= b. Merging, inserting, counting rooms, and removing overlaps are all variations of this check combined with the right sort order and a greedy choice.
Sort meetings by start time. If the next meeting starts before the current one ends, they conflict and need different rooms, or, if you are merging calendars, they become one longer busy block.
When to use it
- Input is a list of ranges, time slots, or segments.
- The problem mentions overlap, merge, conflict, free time, rooms, or arrows.
- You need the minimum number of resources to host all intervals.
- You need the maximum number of non-overlapping intervals.
Problem patterns it solves
Recognize it when: combine overlapping ranges into disjoint blocks; sort by start.
- 56. Merge Intervals
- 57. Insert Interval
- 986. Interval List Intersections
- 759. Employee Free Time
Recognize it when: remove the fewest intervals, or pick the most, so none overlap.
- 435. Non-overlapping Intervals
- 452. Minimum Number of Arrows to Burst Balloons
- 646. Maximum Length of Pair Chain
Recognize it when: maximum number of overlapping intervals at any time.
- 253. Meeting Rooms II
- 2406. Divide Intervals Into Minimum Number of Groups
- 1094. Car Pooling
Recognize it when: can one person attend all meetings.
- 252. Meeting Rooms
- 729. My Calendar I
Recognize it when: is a range fully covered; find missing ranges.
- 1288. Remove Covered Intervals
- 228. Summary Ranges
- 1024. Video Stitching
Where it is used in real software
Google Calendar and Outlook merge busy blocks from multiple calendars to show free time and detect conflicts.
Cloud schedulers compute the peak number of concurrent jobs to size machine pools, the meeting rooms problem.
Interval trees index genomic regions and time ranges for fast overlap queries.
Firewalls merge overlapping CIDR ranges into minimal rule sets.
Key terms
- Overlap
- [a, b] and [c, d] overlap if a <= d and c <= b (use < for half-open intervals).
- Merge
- Replace overlapping intervals with [min start, max end].
- Sort by start
- Used for merging, inserting, and coverage.
- Sort by end
- Used for the greedy maximum set of non-overlapping intervals.
- Sweep line
- Process start and end events in order with a running count.
Merge intervals algorithm
- 1Sort by start
Overlapping intervals now sit next to each other.
- 2Start the result with the first interval
It is the current open block.
- 3Compare each next interval with the last block
If next.start <= last.end, they overlap.
- 4Overlap: extend
last.end = max(last.end, next.end). The max matters when one interval contains another.
- 5No overlap: start a new block
Push the interval to the result.
Merge [[1, 3], [2, 6], [8, 10], [15, 18], [9, 12]]
After sorting by start: [1, 3], [2, 6], [8, 10], [9, 12], [15, 18]
| Interval | Last block | Overlap? | Result after |
|---|---|---|---|
| [1, 3] | - | - | [1, 3] |
| [2, 6] | [1, 3] | 2 <= 3 yes | [1, 6] |
| [8, 10] | [1, 6] | 8 <= 6 no | [1, 6], [8, 10] |
| [9, 12] | [8, 10] | 9 <= 10 yes | [1, 6], [8, 12] |
| [15, 18] | [8, 12] | 15 <= 12 no | [1, 6], [8, 12], [15, 18] |
NOWInterval: [1, 3] | Last block: - | Overlap?: - | Result after: [1, 3]
Three disjoint blocks. Sorting costs O(n log n) and the scan is O(n).
Implementation
function merge(intervals) { intervals.sort((a, b) => a[0] - b[0]); const result = [intervals[0].slice()]; for (let i = 1; i < intervals.length; i++) { const last = result.at(-1); const [start, end] = intervals[i]; if (start <= last[1]) last[1] = Math.max(last[1], end); else result.push([start, end]); } return result;} // 435: minimum removals = n - maximum non-overlapping set (greedy by end)function eraseOverlapIntervals(intervals) { intervals.sort((a, b) => a[1] - b[1]); let kept = 0, lastEnd = -Infinity; for (const [start, end] of intervals) { if (start >= lastEnd) { kept++; lastEnd = end; // the earliest finish leaves the most room } } return intervals.length - kept;} // 253: minimum meeting rooms with two sorted arraysfunction minMeetingRooms(intervals) { const starts = intervals.map((i) => i[0]).sort((a, b) => a - b); const ends = intervals.map((i) => i[1]).sort((a, b) => a - b); let rooms = 0, best = 0, e = 0; for (let s = 0; s < starts.length; s++) { if (starts[s] < ends[e]) rooms++; // needs a new room else e++; // reuse a room that just freed up best = Math.max(best, rooms); } return best;}Complexity and performance
Dominates most interval solutions.
Single pass after sorting.
Heap of end times.
No sort needed.
Trade-offs
Merging needs start order to bring overlaps together. Selecting the most non-overlapping intervals needs end order, because finishing earliest leaves the most room.
Both are O(n log n). Two sorted arrays is shorter; the heap also tells you which meeting ends next.
Whether [1, 2] and [2, 3] overlap depends on the problem; check with an example before coding.
Variants and related techniques
Two pointers over two sorted lists: overlap is [max(starts), min(ends)] when valid; advance the one that ends first.
Turn intervals into +1 / -1 events; the running maximum is the peak overlap.
For online insertions and overlap queries, use a balanced tree keyed by start (My Calendar).
Common mistakes
- Forgetting max when merging.
Fix: [1, 10] and [2, 3] merge to [1, 10], not [1, 3].
- Mutating the input intervals.
Fix: Copy the first interval when building results if the caller needs the input.
- Comparator overflow in Java.
Fix: Use Integer.compare(a[0], b[0]) rather than a[0] - b[0].
Interview questions
Why sort by end time for the maximum non-overlapping set?
Picking the interval that ends earliest leaves the most room for the rest. An exchange argument shows any optimal solution can swap its first interval for the earliest-ending one without losing count.
How do you find the minimum number of meeting rooms?
It equals the maximum number of meetings overlapping at any moment. Compute it with a min-heap of end times or a sweep over sorted start and end times.
Practice problems
| Problem | Difficulty | What it trains |
|---|---|---|
| 252. Meeting Rooms | Easy | Conflict check. |
| 56. Merge Intervals | Medium | Sort and merge. |
| 57. Insert Interval | Medium | Three phases. |
| 435. Non-overlapping Intervals | Medium | Greedy by end. |
| 452. Minimum Number of Arrows to Burst Balloons | Medium | Greedy by end. |
| 253. Meeting Rooms II | Medium | Heap or sweep. |
| 986. Interval List Intersections | Medium | Two pointers. |