Meetings, bookings, train timings, exam slots: any time a problem gives you start and end times, think merge intervals. It's a short, friendly pattern with one rule to remember, and it appears often at Google, Meta and Indian product companies (think booking and delivery-slot apps).
The 12 interview patterns: you are here
- 1Two Pointerssorted + pair
- 2Sliding Windowcontiguous subarray
- 3Prefix Sum + Hash Maprange sums, sum = K
- 4Binary Search on Answersmallest value that works
- 5Fast & Slow Pointerscycle, middle
- 6Merge Intervalsoverlapping ranges
- 7Monotonic Stackcoming soon
- 8Heap / Top-Kcoming soon
- 9Tree Patternscoming soon
- 10Grid BFS & Topological Sortcoming soon
- 11Backtrackingcoming soon
- 12DP Patternscoming soon
The seminar hall story
Your college seminar hall has bookings from different clubs: 9–11, 10–12, 2–3. The office wants to know when the hall is actually busy. If you sort the bookings by start time, it's easy: 9–11 and 10–12 overlap (10 comes before 11), so the hall is busy 9–12. Then 2–3 is separate.
Add your own bookings below, then sort and merge them:
📅 Merge the seminar hall bookings
Bookings (as they came in)
Sorting is the secret. Once bookings are in start-time order, any overlap is always with the block you just built, so one pass is enough. Total time: O(n log n) for the sort.
The only rule you need
After sorting: next.start ≤ last.end → overlap
Then last.end = max(last.end, next.end). The max matters: [1, 10] swallows [2, 3] completely.
🔍 Use merge intervals when you see
- ✓The input is a list of [start, end] pairs
- ✓Meetings, bookings, time slots, ranges on a line
- ✓“Overlapping”, “merge”, “free time”, “conflict”
- ✓“Can one person attend all?”, “insert a new interval”
🚫 Don't use it when
- ✗Single numbers, not ranges (think two pointers or hashing)
- ✗“How many rooms at once?”: that needs a heap of end times too
- ✗Ranges of an array's indexes for sums (prefix sum)
The template
intervals.sort(by start)
merged = [intervals[0]]
for start, end in intervals[1:]:
if start <= merged[-1].end: # overlap
merged[-1].end = max(merged[-1].end, end) # stretch
else:
merged.append([start, end]) # new block⚡ Memory tricks
- 1
“Sort by start, then glue”
Always step 1 for interval problems.
- 2
“Starts before it ends? They're friends.”
next.start ≤ last.end means overlap.
- 3
“Stretch to the max”
Use max(last.end, next.end), never just next.end.
- 4
“Sort costs n log n, the glue is free”
The merge pass is O(n); the sort dominates.
Code: merge intervals and meeting rooms (Python, Java, C++)
Outputs in the comments come from running the code.
def merge(intervals):
"""Sort by start, then glue each interval onto the last one if they overlap."""
intervals.sort(key=lambda x: x[0])
merged = [intervals[0]]
for start, end in intervals[1:]:
last = merged[-1]
if start <= last[1]: # overlaps: stretch the last one
last[1] = max(last[1], end)
else: # gap: start a new block
merged.append([start, end])
return merged
def can_attend_all(meetings):
"""True if no two meetings overlap."""
meetings.sort(key=lambda x: x[0])
for i in range(1, len(meetings)):
if meetings[i][0] < meetings[i - 1][1]: # starts before the previous one ends
return False
return True
print(merge([[8, 10], [1, 3], [2, 6], [15, 18]])) # [[1, 6], [8, 10], [15, 18]]
print(merge([[1, 4], [4, 5]])) # [[1, 5]] (touching counts as overlap)
print(can_attend_all([[0, 30], [5, 10], [15, 20]])) # False
print(can_attend_all([[7, 10], [2, 4]])) # TrueOne pattern, many problems
| Famous problem | How the pattern solves it | Level |
|---|---|---|
| Meeting Rooms | Sort, check if any start < previous end | Easy |
| Merge Intervals | Sort, stretch on overlap | Medium |
| Insert Interval | Add before, merge overlaps, add after | Medium |
| Non-overlapping Intervals | Sort by end, count what you must remove | Medium |
| Minimum Arrows to Burst Balloons | Sort by end, one arrow per group | Medium |
| Interval List Intersections | Two pointers over two sorted lists | Medium |
| Meeting Rooms II | Sort + min-heap of end times | Medium |
🎯 Spot the pattern: is it merge intervals?
In an interview nobody tells you the pattern. Read each problem and decide. Look for the signal words.
1. Given meeting times, can one person attend all of them?
2. Insert a new booking into a sorted list of bookings and merge if needed.
3. Find the longest substring without repeating characters.
4. Minimum number of arrows to burst balloons given their [start, end] ranges.
5. Detect whether a linked list has a loop.
🧠 PATH memory card
Remember merge intervals for good
Picture it · Anchor it · Trace it · Hit-recall it. Four quick steps that turn “I read it once” into “I can explain it in an exam”.
- P
Picture it
Your brain remembers scenes far better than definitions.
← swipe to see the whole picture →
Seminar hall bookings sorted by start time: 9–11 and 10–12 overlap, so the hall is busy 9–12. 2–3 is separate.
- A
Anchor it
A silly sentence you can't forget, where every word maps to the concept.
“Seminar hall: sort by start; starts before it ends? stretch to the max”
- Seminar hall→A list of [start, end] intervals
- Sort by start→intervals.sort(key = start): step 1
- Starts before it ends→next.start ≤ last.end → overlap
- Stretch to the max→last.end = max(last.end, next.end)
- Otherwise→Gap → append a new block
- T
Trace it
Run it by hand on a tiny example once. Doing beats reading.
[[8,10],[1,3],[2,6],[15,18]]→ sorted[1,3],[2,6],[8,10],[15,18]. 2 ≤ 3 → [1,6] · 8 > 6 → new [8,10] · 15 > 10 → new [15,18]. Result: [1,6], [8,10], [15,18]. - H
Hit-recall it
Test yourself now, then again on the day each card comes back.
Card 1 / 6 · new0 mastered · 0 practisedSay the answer out loud first, then flip and be honest.
Common mistakes
- Not sorting first. Unsorted input makes you miss overlaps between far-apart intervals.
- Setting
end = next.end. Always take the max, or a short interval inside a long one shrinks it. - Getting touching wrong. Read the problem: are [1,4] and [4,5] overlapping? Use
≤if yes,<if no.
You've learned 6 interview patterns 🎉
Two pointers, sliding window, prefix sum, binary search on the answer, fast and slow pointers, and merge intervals. Together they cover a big share of the array, string and linked-list questions in coding interviews. Revise them with the flashcards in each lesson; a few minutes a day keeps them sharp.
Next up in the course: monotonic stack, heaps and top-K, tree patterns, grid BFS and topological sort, backtracking and DP patterns, plus a “Which pattern?” challenge. See what's coming on the DSA course page, and join the list below to get them by email.
Key takeaways
- ✓Interval problems: sort by start first.
- ✓Overlap after sorting: next.start ≤ last.end.
- ✓Merge by stretching: last.end = max(last.end, next.end).
- ✓O(n log n) total, dominated by the sort.
- ✓Signals: start/end pairs, meetings, bookings, overlap, free time.
Frequently asked questions
How do you merge overlapping intervals?
Sort the intervals by start time. Keep a result list with the first interval. For each next interval, if its start is ≤ the end of the last merged interval, they overlap: stretch the end to the bigger of the two ends. Otherwise, add it as a new interval. Time O(n log n) because of the sort.
When do two intervals overlap?
Intervals [a, b] and [c, d] overlap when each starts before the other ends: a ≤ d and c ≤ b. After sorting by start, you only need one check: the next start ≤ the current end. Whether touching intervals like [1, 4] and [4, 5] count as overlapping depends on the problem; in LeetCode's Merge Intervals they do.
Why do we sort intervals by start time?
Sorting puts intervals that could overlap next to each other. Then each new interval can only overlap with the block you just built, never with an older one, so a single pass is enough.
What is the time complexity of merge intervals?
O(n log n) for sorting plus O(n) for the single pass, so O(n log n) overall. The output list can hold up to n intervals, so space is O(n) (or O(log n) extra for the sort if you don't count the output).
How is the meeting rooms problem related?
Meeting Rooms asks whether one person can attend all meetings: sort by start and check if any meeting starts before the previous one ends. Meeting Rooms II asks how many rooms you need at the same time, which adds a min-heap of end times (coming in the Heap / Top-K lesson).
🧠 Quick check: merge intervals
1. What's the very first step in merge intervals?
2. Last merged block is [2, 6]. Next interval is [5, 9]. What happens?
3. Last merged block is [1, 10]. Next is [2, 3]. What's the new end?
4. Meetings [0, 30], [5, 10], [15, 20]. Can one person attend all?
Checking your access…