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 →Rooms needed at any instant equals meetings in progress at that instant, so the answer is the maximum concurrency over the timeline.
The sweep-line version: pull the start times and end times into two separate sorted arrays, then walk them with two pointers. Each start increments a counter, each end decrements it, and the answer is the largest value the counter reaches. Decoupling starts from ends is the trick - after sorting, it no longer matters which start paired with which end, only how they interleave.
When a start and an end share a timestamp, process the end first, so the freed room is reused rather than a new one allocated. That is the difference between `[[1,2],[2,3]]` needing 1 room and 2.
The equivalent min-heap formulation keeps the end times of active meetings and pops those that have finished; both are O(n log n) and interviewers accept either. The two-pointer version avoids the heap entirely.
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.