- Interview Questions
- /
- Problems
- /
- Meeting Rooms II
Meeting Rooms II
Given meeting time ranges, find the fewest rooms that can host all of them. The question is really asking for the peak number of overlapping intervals, solved with sorting plus a heap or a sweep. Solve it below in Python or JavaScript, then read the walkthrough.
Problem
You're given an array intervals where intervals[i] = [start, end] is the time range of one meeting. A meeting occupies a room from start up to, but not including, end.
Return the minimum number of conference rooms needed so that every meeting can be held and no room ever hosts two meetings at once.
Because end is exclusive, a meeting that ends at time t and another that starts at time t can use the same room back to back. The intervals are in no particular order.
Example 1
- Input
- intervals = [[9, 12], [10, 11], [11, 14]]
- Output
- 2
[9, 12] and [10, 11] overlap, so they need two rooms. [11, 14] can take the room [10, 11] just freed.
Example 2
- Input
- intervals = [[1, 4], [4, 6], [6, 8]]
- Output
- 1
Each meeting starts exactly when the previous one ends, so one room is enough.
Example 3
- Input
- intervals = [[3, 10], [5, 7], [6, 9], [1, 4]]
- Output
- 3
Between 6 and 7, the meetings [3, 10], [5, 7], and [6, 9] are all running.
Constraints
- 1 ≤ intervals.length ≤ 10⁴
- intervals[i].length = 2
- 0 ≤ start < end ≤ 10⁶
Practice
Runs 12 tests in your browser. Python loads on the first run (about 6 MB, then cached). Press ⌘/Ctrl + Enter to run.
Get asked Meeting Rooms II in a real interview?
Interview Solver gives you a working solution live, during your actual coding interview. Invisible to screensharing.
How to solve Meeting Rooms II
Pattern: Sweep line over start and end events
The number of rooms you need is exactly the largest number of meetings running at the same moment. You need at least that many, since those meetings all need separate rooms, and that many is always enough, since you can hand a freed room to the next meeting that starts.
So stop thinking about rooms and count overlaps. Sort all the start times and, separately, all the end times. Walk through the starts in order. For each meeting that starts, first check whether the earliest unfinished meeting has already ended (its end ≤ this start). If so, that room frees up and the count doesn't grow. Otherwise you need one more room.
The heap version is the same idea told differently: keep a min-heap of end times for rooms in use, and reuse the room at the top of the heap whenever it is free by the time the next meeting starts.
Approach 1: Check every time point
For every start time, count how many meetings are running at that moment by scanning all intervals, and take the maximum. The peak always happens at some start time, so this is correct, but it compares every pair.
Time O(n²) · Space O(1)
Approach 2: Sort by start, min-heap of end times
Sort meetings by start. For each meeting, if the smallest end time in the heap is ≤ its start, pop it (that room is free). Then push this meeting's end. The heap size at the end is the answer, because the heap never shrinks below the peak.
Time O(n log n) · Space O(n)
Approach 3: Two sorted arrays (chronological sweep)
- Put all starts in one sorted array and all ends in another.
- Walk the starts with index
iand keep an end pointerj, plusroomsandbest. - For each start: if
starts[i] ≥ ends[j], a meeting finished, so decrementroomsand advancej. Then incrementroomsfor the new meeting and updatebest.
You never need to know which end belongs to which start. Only the counts matter.
Time O(n log n) · Space O(n)
| Approach | Time | Space |
|---|---|---|
| Check every time point | O(n²) | O(1) |
| Sort by start, min-heap of end times | O(n log n) | O(n) |
| Two sorted arrays (chronological sweep) | O(n log n) | O(n) |
Solution code
Try the practice editor first. Both solutions pass every test above.
Meeting Rooms II solution in Python
def min_meeting_rooms(intervals: list[list[int]]) -> int:
starts = sorted(s for s, _ in intervals)
ends = sorted(e for _, e in intervals)
rooms = best = j = 0
for start in starts:
if start >= ends[j]:
rooms -= 1
j += 1
rooms += 1
best = max(best, rooms)
return bestMeeting Rooms II solution in JavaScript
function minMeetingRooms(intervals) {
const starts = intervals.map((iv) => iv[0]).sort((a, b) => a - b);
const ends = intervals.map((iv) => iv[1]).sort((a, b) => a - b);
let rooms = 0;
let best = 0;
let j = 0;
for (const start of starts) {
if (start >= ends[j]) {
rooms--;
j++;
}
rooms++;
best = Math.max(best, rooms);
}
return best;
}Common mistakes
- Treating a meeting that ends at t and one that starts at t as overlapping. With exclusive ends, the comparison is end ≤ start, not end < start.
- Sorting only by start and comparing each meeting to the previous one, which misses a long meeting that overlaps several later ones.
- Returning the number of rooms in use at the end of the sweep instead of the peak, in the two-array version.
- In the heap version, pushing the new end before popping the free room, which can make the heap grow when it shouldn't.
What interviewers look for
Reframe the problem out loud first: "the minimum number of rooms equals the maximum number of simultaneous meetings." Interviewers listen for that reduction, and it makes both the heap and sweep solutions feel obvious.
Ask whether back-to-back meetings can share a room. It changes a single comparison, and settling it early shows care. If you use the two-array trick, explain why it is safe to separate starts from ends: you only count how many meetings are active, not which ones.
Follow-up questions
Can one person attend all the meetings (Meeting Rooms I)?
Sort by start and check that each meeting starts at or after the previous one ends. That is O(n log n), and it is the same as asking whether the answer here is 1.
How would you also assign each meeting to a specific room?
Use the heap version but store (end time, room id) pairs. When a room frees up, reuse its id; otherwise create a new id. Record the id for each meeting as you go. Keeping a second heap of free room ids lets you always pick the lowest-numbered free room.
What if the time range is small, like minutes in one day?
Use a difference array: add 1 at each start and subtract 1 at each end, then take a running prefix sum and its maximum. That is O(n + T) with no sorting, where T is the number of time slots.
Companies that ask Meeting Rooms II
Meeting Rooms II is tagged by 117 companies in our interview dataset. The score shows how often it comes up at each company.
- Lenovo100%
- Skyworks Solutions100%
- Cloud Software Group100%
- Worldquant100%
- Snap98.6%
- Freshworks92%
- Liberty It88%
- Splunk87.7%
- Disney87.6%
- Netflix85.5%
- Draftkings84%
- Costco Wholesale84%
- TikTok83.8%
- Figma83%
- Ikea83%
- Ellucian83%
- Logmein82%
- Paxos81%
- Unacademy80%
- Dassault Systèmes80%
- Waymo79.2%
- Personio79%
- Mimecast79%
- Basis Technologies78%
and 93 more.
Practice next
Get live help in the real interview
Working solutions in real time during your actual interview. Invisible to screensharing.
Start Free TrialNo credit card required