Pattern · 6 problems
Interval Interview Problems
Interval problems get easy after one sort. I order by start time, then ask whether each range overlaps the last.
Free: write the code and run it, no sign-up.
When I reach for it
- The input is ranges: meetings, bookings or segments.
- The question asks about overlaps, merges or gaps.
- Sorting by start time would make the answer visible.
Problems, easiest first
1 easy · 4 medium · 1 hard. Open one to read it, then write it in the free editor.
The idea
Sort by start time. After that, each interval either overlaps the last one you kept or begins after it. There is no third case.
Decide whether touching ranges overlap before you code. [1, 2] and [2, 3] flip a lot of answers.
intervals.sort(key=lambda x: x[0]) # sort by start
merged = [intervals[0]]
for start, end in intervals[1:]:
if start <= merged[-1][1]: # overlaps the last kept one
merged[-1][1] = max(merged[-1][1], end)
else:
merged.append([start, end])Where it goes wrong
- Not deciding whether touching ranges overlap.
- Forgetting the max when a long range swallows a short one.
- Skipping the empty input.
Say this before you type. Say what you sort by, and whether two ranges that touch count as overlapping.
Where Coco comes in
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit, and to talk with her for one hour. After that, talking comes with the $79 pack. Anyone can open the editor and run their code.
Questions people ask
- Why sort intervals by start time?
- After sorting, an overlap can only happen with the last interval you kept. One pass finds every merge.
- When do I sort by end time instead?
- When you pick as many non-overlapping intervals as you can. Taking the earliest finish leaves the most room for the rest.
Meet these next
Fin and Coco are characters, not people. strongyes.io is independent. Leo builds it with AI agents.
