Intervals: how to spot it
We'll cover the following
Each item is a range, such as a meeting or a time slot. Sort the ranges, then sweep through once and decide how each range relates to the last one.
How the question sounds
- Merge overlapping ranges or time slots
- Minimum meeting rooms, or the most overlap at one time
- Insert a range into a sorted list of ranges
- Fewest removals so ranges do not overlap
- Input is a list of [start, end] pairs
The two shapes
Where it shows up in GenAI work
Requests are batched into time windows, and GPU jobs and reservations are scheduled as intervals. Merging overlapping text spans or chunk ranges is the same idea, and so is checking how many concurrent requests a service must handle at peak.
Is it intervals?
Decide yes or no before you open each answer.
Check yourselfMerge all overlapping meeting times.
Yes. Sort by start and sweep.
Check yourselfFind the minimum number of meeting rooms needed.
Yes. Track the earliest room end time while sweeping by start.
Check yourselfRemove the fewest intervals so none overlap.
Yes. Greedy: sort by end and keep what fits.
Check yourselfFind the longest substring with no repeats.
No. A sliding window.
Check yourselfList every permutation of an array.
No. Backtracking.
Check yourselfFind the kth largest number.
No. A heap.
Problems in this pattern
- Merge intervals · Medium · Sort then sweep
- Insert interval · Medium · Three phases
- Meeting rooms · Medium · Sweep with end times
- Fewest removals to avoid overlap · Medium · Greedy by end
Summary: say it in the interview
- Sort by start: Overlapping ranges become neighbours, so you can merge them in one sweep.
- Sort by end: For "keep as many as possible" problems, always take the range that ends first.