Managing overlapping timelines, meeting rooms, and range merges
41 problems·3 Easy·27 Medium·11 Hard
Pattern Study Guide & Cheat Sheet· O(n log n) dominated by initial sort · O(n) for output list
▼
Sort ranges by start (or end) time so overlapping relationships can be resolved linearly by comparing adjacent pairs.
Core Invariant: Once sorted by start time (`start_i <= start_j`), interval J overlaps with interval I if and only if `start_j <= end_i`. When overlapping, merged end is `max(end_i, end_j)`.
5.Else: no overlap. Append `[curr_start, curr_end]` to `merged`.
6.Return `merged`.
Watch for (Interview Traps)
Touching endpoints edge case: verify if `[1, 2]` and `[2, 3]` overlap (`start <= last_end` vs `start < last_end`)
Forgetting `max(last_end, end)`: the previous interval might already be longer than current
Meeting Rooms II: sorting only by start time without tracking end times with a heap
Merge Overlapping Intervals
def merge_intervals(intervals: list[list[int]]) -> list[list[int]]:
if not intervals:
return []
# Sort primarily by start time
intervals.sort(key=lambda x: x[0])
merged = [intervals[0]]
for start, end in intervals[1:]:
last_end = merged[-1][1]
if start <= last_end:
# Overlapping intervals: merge by taking the max end
merged[-1][1] = max(last_end, end)
else:
# Non-overlapping: append as separate interval
merged.append([start, end])
return merged
Cost
O(n log n) dominated by initial sort · O(n) for output list (Linear O(n) pass after the O(n log n) sort.)
Canonical problems
#56 Merge Intervals:Standard sort by start time and extend ends
#57 Insert Interval:Add before overlap, merge during overlap, append after overlap
#435 Non-overlapping Intervals:Greedy: sort by end time to keep intervals that finish earliest
#253 Meeting Rooms II:Min-heap of end times tracks currently occupied rooms
What is the Intervals & Overlap Scheduling pattern in coding interviews?
Managing overlapping timelines, meeting rooms, and range merges It is categorized under Data Structures and features 41 curated interview problems across Easy (3), Medium (27), and Hard (11) difficulties.
Which tech companies frequently test Intervals & Overlap Scheduling?
How do I master the Intervals & Overlap Scheduling pattern?
Start by studying the algorithmic strategy and template code, then practice Easy and Medium problems before attempting Hard problems under timed conditions.