Interval Problems in Coding Interviews: Merge, Insert, and Overlap
Learn the core interval patterns for interview problems: merge intervals, insert interval, and meeting rooms with Python walkthroughs.
Why interval problems show up so often
Interval problems are a favorite in coding interviews because they test more than one skill at once:
- Can you sort data in a useful way?
- Can you reason about boundaries and overlap?
- Can you reduce a problem to a simple sweep through ordered events?
That makes them feel different from pure array or string problems, but the core pattern is often the same. Once you learn to think in terms of start times, end times, and overlap, a lot of these problems become straightforward.
Three of the most common interval tasks are:
- Merge Intervals: combine overlapping ranges
- Insert Interval: add one range into a sorted, non-overlapping list
- Meeting Rooms: determine whether intervals conflict or how many rooms are needed
If you can solve these cleanly, you are already covering a big chunk of interval interview questions.
Problem statement
At a high level, interval problems ask you to work with ranges like [start, end].
Common tasks include:
-
Merge intervals
- Input: a list of intervals that may overlap
- Output: a list where all overlapping intervals are combined
-
Insert interval
- Input: a sorted list of non-overlapping intervals and one new interval
- Output: the updated list after inserting and merging as needed
-
Meeting rooms / overlap detection
- Input: meeting intervals
- Output: whether one person can attend all meetings, or the minimum number of rooms needed
The exact output differs, but the decision points are very similar:
- Does the next interval start before the current one ends?
- If so, should you merge, reject, or allocate another resource?
The core insight
The key insight is that sorting by start time exposes the structure.
Once intervals are sorted:
- Overlaps only happen between neighboring intervals in the sorted order
- You can maintain a running interval or running end time
- You usually do not need nested loops
Overlap rule
For two intervals a = [s1, e1] and b = [s2, e2]:
- They overlap if
s2 <= e1whenacomes beforebin sorted order - If intervals are half-open, the rule may become
s2 < e1
Most interview questions use closed intervals unless stated otherwise, so be careful about edge cases like [1, 2] and [2, 3].
Approach 1: Merge intervals
Idea
Sort intervals by start time. Then walk through them and keep a result list.
For each interval:
- If it does not overlap with the last merged interval, append it
- If it overlaps, merge by extending the end time
Python code
from typing import List
def merge_intervals(intervals: List[List[int]]) -> List[List[int]]:
if not intervals:
return []
intervals.sort(key=lambda x: x[0])
merged = [intervals[0]]
for start, end in intervals[1:]:
last_start, last_end = merged[-1]
if start <= last_end:
merged[-1][1] = max(last_end, end)
else:
merged.append([start, end])
return mergedWalkthrough example 1
Input:
intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]Sorted order is the same. Now step through:
| Current interval | Merged so far | Action |
|---|---|---|
| [1, 3] | [[1, 3]] | Initialize |
| [2, 6] | [[1, 3]] | Overlaps, extend to [1, 6] |
| [8, 10] | [[1, 6]] | No overlap, append |
| [15, 18] | [[1, 6], [8, 10]] | No overlap, append |
Result:
[[1, 6], [8, 10], [15, 18]]Why this works
After sorting, if an interval overlaps with anything, it will overlap with the last interval in the merged list. You do not need to compare it against earlier merged intervals because those are already disjoint and ordered.
Approach 2: Insert interval
Idea
The insert problem is a small twist on merge intervals.
You are given:
- a sorted list of non-overlapping intervals
- a new interval to insert
The clean way is to process the existing intervals in three phases:
- Add all intervals that end before the new interval starts
- Merge all overlapping intervals with the new interval
- Add all intervals that start after the new interval ends
Python code
from typing import List
def insert_interval(intervals: List[List[int]], new_interval: List[int]) -> List[List[int]]:
result = []
i = 0
n = len(intervals)
# 1) Add intervals completely before new_interval
while i < n and intervals[i][1] < new_interval[0]:
result.append(intervals[i])
i += 1
# 2) Merge overlaps with new_interval
while i < n and intervals[i][0] <= new_interval[1]:
new_interval[0] = min(new_interval[0], intervals[i][0])
new_interval[1] = max(new_interval[1], intervals[i][1])
i += 1
result.append(new_interval)
# 3) Add remaining intervals
while i < n:
result.append(intervals[i])
i += 1
return resultWalkthrough example 2
Input:
intervals = [[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]]
new_interval = [4, 8]Step by step:
[1, 2]ends before4, so keep it[3, 5]overlaps with[4, 8], merge to[3, 8][6, 7]overlaps with[3, 8], merge stays[3, 8][8, 10]overlaps because8 <= 8, merge to[3, 10][12, 16]starts after the merged interval, keep it
Result:
[[1, 2], [3, 10], [12, 16]]Why this works
The input list is already sorted and non-overlapping, which removes a lot of complexity. You only need one pass because the new interval can only interact with a contiguous block of intervals.
Approach 3: Meeting rooms and overlap counting
Meeting room questions come in two common forms:
- Can one person attend all meetings?
- What is the minimum number of rooms needed?
3A. Can one person attend all meetings?
Sort intervals by start time and check whether any meeting starts before the previous one ends.
from typing import List
def can_attend_meetings(intervals: List[List[int]]) -> bool:
intervals.sort(key=lambda x: x[0])
for i in range(1, len(intervals)):
if intervals[i][0] < intervals[i - 1][1]:
return False
return TrueWalkthrough example 3
Input:
intervals = [[0, 30], [5, 10], [15, 20]]After sorting:
- Compare
[5, 10]with[0, 30]→5 < 30, overlap exists - Return
False
This is the simplest overlap check you can write.
3B. Minimum number of meeting rooms
This version asks how many overlaps happen at the same time.
A common solution uses two sorted arrays: one for starts and one for ends.
Python code
from typing import List
def min_meeting_rooms(intervals: List[List[int]]) -> int:
if not intervals:
return 0
starts = sorted(interval[0] for interval in intervals)
ends = sorted(interval[1] for interval in intervals)
start_ptr = 0
end_ptr = 0
rooms = 0
max_rooms = 0
while start_ptr < len(intervals):
if starts[start_ptr] < ends[end_ptr]:
rooms += 1
max_rooms = max(max_rooms, rooms)
start_ptr += 1
else:
rooms -= 1
end_ptr += 1
return max_roomsWhy this works
Think of it as a sweep line:
- A new meeting starts: you need a room
- A meeting ends: you free a room
By sorting starts and ends separately, you can process events in order without building an explicit event list.
How to choose the right interval strategy
You do not need one universal template, but these patterns cover most cases.
| Problem type | Best approach | Key idea | Time complexity | Space complexity |
|---|---|---|---|---|
| Merge intervals | Sort + single pass | Extend the last merged interval | O(n log n) | O(n) |
| Insert interval | Three-phase scan | Before, overlap, after | O(n) | O(n) |
| Can attend meetings | Sort + adjacent comparison | Check for any overlap | O(n log n) | O(1) to O(n) |
| Min meeting rooms | Two sorted arrays / sweep line | Track starts vs ends | O(n log n) | O(n) |
Common edge cases
You should always test these:
- Empty input
- One interval only
- Intervals that touch at endpoints, like
[1, 2]and[2, 3] - Fully nested intervals, like
[1, 10]and[2, 3] - Many intervals with the same start or end times
Endpoint behavior matters
For example:
- If the problem treats intervals as closed,
[1, 2]and[2, 3]overlap - If the problem treats them as half-open, they do not
Read the statement carefully. This is a common place to lose points in an interview.
A simpler mental model
When you see intervals, ask yourself three questions:
- Can sorting expose a local decision?
- Do I only need to compare with the previous interval?
- Am I merging, rejecting, or counting overlap?
If the answer to the first two is yes, you probably want a linear scan after sorting.
Related problems
Once you understand interval merging and overlap, you will start recognizing related patterns:
- Non-overlapping Intervals — remove the minimum number of intervals to eliminate overlap
- Employee Free Time — merge schedules and find gaps
- Interval List Intersections — find overlap between two interval lists
- Car Pooling — track passenger count with a sweep line or difference array
- Minimum Number of Arrows to Burst Balloons — a greedy interval covering problem
These problems often reuse the same ideas:
- sort by start or end
- sweep from left to right
- keep one or two pointers
- maintain the active range or active count
Practical tips for interviews
- Start by stating whether intervals overlap when
start <= previous_endorstart < previous_end - Explain your sorting choice before writing code
- Prefer a one-pass scan after sorting
- Keep the code simple enough to reason about under pressure
- Test with a small example out loud
A good interview answer is not just correct; it is easy to verify.
Key takeaways
- Sorting is usually the first move for interval problems.
- Merge problems usually need one pass after sorting.
- Insert interval is just merge intervals with a structured scan.
- Meeting room problems are about tracking overlap over time.
- Pay close attention to endpoint rules.
Practice these next
If you want to get comfortable with the pattern, try these problems next:
- Merge Intervals
- Insert Interval
- Meeting Rooms
- Meeting Rooms II
- Interval List Intersections
If you'd like to see the variables update live as you step through, check out codebrew.ai.

