Interval Scheduling
Software Engineer path · lesson 33 of 99Master 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."
Times 0–24, like 1-4, 3-5. Up to 12.
Sort by start: [1,3] [2,6] [6,7] [8,10] [15,18].
- 1sort intervals by start
- 2merged = [first]
- 3for cur in the rest:
- 4 if cur.start <= merged[-1].end: merged[-1].end = max(merged[-1].end, cur.end) # overlap
- 5 else: merged.append(cur)
- Comparisons
- 0 / 4
- Merges
- 0 / 2
Tap any element in the diagram to inspect its state at this step.
Keys: ← → step, Space play/pause. Predict mode pauses before key steps and asks what happens next.
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
function mergeIntervals(intervals) {
// Your code here
}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
References & Further Reading
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