We use cookies for site analytics. Accept to help us understand how the site is used. See our Privacy Policy for details.
A medium intervals problem, graded against 6 test cases (3 of them hidden).
Sorting by start or end, then merging or scheduling overlapping ranges.
Reach for it when you see: Meetings, bookings, ranges, or anything with a start and an end.
More Intervalsproblems →For any pair of intervals, the overlap is `[max(startA, startB), min(endA, endB)]`, and it exists only if that low bound does not exceed that high bound. Because these are closed intervals the test is `<=`, not `<` - which is why `[1,3]` and `[3,5]` produce the degenerate but valid `[3,3]`.
The pairing strategy is what makes it linear. Keep one pointer per list, test the current pair, and then advance the pointer whose interval has the smaller end. That interval is finished: everything remaining in the other list starts at or after the current position and ends later, so it cannot intersect anything else. Advancing the other pointer instead would skip real intersections.
Each step consumes one interval, so the walk is O(m + n) - no sorting, since both inputs arrive sorted.
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
Read off this problem's own test suite, so these are the cases a submission actually has to survive.
These apply to the pattern as a whole, not just this problem.