Interval Scheduling
Master the Merge Intervals pattern: sort by start time, then sweep once to merge every overlapping range into the minimal covering set. Shows up constantly in FAANG interviews as "Merge Intervals," "Meeting Rooms," and "Insert Interval."
Interval Scheduling: Merge Intervals
Master the Merge Intervals pattern: sort by start time, then sweep once to merge every overlapping range into the minimal covering set. Shows up constantly in FAANG interviews.
Merge Intervals vs Activity Selection
Merge Intervals (this topic): combine overlapping ranges into a minimal covering set — nothing is discarded, everything is merged. Sort by start time. Activity Selection (Greedy Algorithms topic): select the maximum count of non-overlapping intervals, discarding conflicts. Sort by end time. Both sweep linearly after the sort.
Input: 6 intervals with [start, end] time ranges. Goal: merge all overlapping intervals into the minimal covering set.
Merge the intervals
Merge [1,3], [2,6], [8,10], [9,12], [15,18]. Fill in the merged result in order.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
Test your knowledge
What is the first step in the Merge Intervals algorithm?
Write the code
Merge Intervals
The short version
Picture a wall calendar covered in overlapping sticky notes representing scheduled meetings. Your goal is to consolidate every note that shares at least one minute with a neighbour into a single, bigger note — leaving only non-touching blocks behind. You pick up notes in order of their start time and ask: "Does this note touch or overlap the last block on my wall?" If yes, you extend that block (merge). If no, you pin a new block. One pass, done.
In more depth
Merging Intervals
An interval is a pair [start, end] — a meeting, a booking, a range of line numbers. Interval problems ask how a set of them overlaps: merge the overlapping ones, count how many rooms are needed, or find the largest set that never conflicts.
Why sorting comes first
Unsorted, deciding whether any two intervals overlap is O(n²). Sorted by start time, the problem collapses: while sweeping left to right you only ever need to compare the current interval against the one you are building, because anything that could overlap it starts at or after the current position.
The merge sweep
Hold an open interval. For each next interval, if its start is at or before the open interval's end, the two touch — extend the open end to the larger of the two ends. Otherwise the open interval is finished: emit it and open a new one. One sort plus one pass gives O(n log n), dominated by the sort.
The variants share the skeleton
Sort by end time instead and greedily keep the earliest-finishing compatible interval, and you have maximum non-overlapping scheduling. Sweep starts and ends as separate +1/−1 events and the running total is the number of rooms needed at any moment.
Interview Questions
Love this visualization?
Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.
100% free • No credit card required