253. Meeting Rooms II
Asked at Google, Meta, Amazon, Microsoft and 6 more
Difficulty
Medium
Time
O(n log n)
Space
O(n)
Pattern
Array
Companies
10
Problem
You are given a list of meeting time intervals. Each interval is a pair [start, end], meaning a meeting begins at start and ends at end.
Return the minimum number of conference rooms needed so that every meeting can happen. Two meetings can share one room only if they do not overlap in time. A meeting that ends at time t and another that starts at time t do not overlap, so they can use the same room.
In short: find the largest number of meetings that are happening at the same moment. That number is the answer.
How many meetings overlap at once?
Constraints
- ●
1 <= intervals.length <= 10^4 - ●
0 <= start_i < end_i <= 10^6
Examples
intervals = [[0,30],[5,10],[15,20]]2intervals = [[7,10],[2,4]]1Asked At
10 companies| Company | Difficulty | |
|---|---|---|
| Medium | View all Google questions → | |
| Meta | Medium | View all Meta questions → |
| Amazon | Medium | View all Amazon questions → |
| Microsoft | Medium | View all Microsoft questions → |
| Apple | Medium | View all Apple questions → |
| Netflix | Medium | View all Netflix questions → |
| Uber | Medium | View all Uber questions → |
| Salesforce | Medium | View all Salesforce questions → |
| TikTok | Medium | View all TikTok questions → |
| JPMorgan | Medium | View all JPMorgan questions → |
How to Think About It
Sort meetings by start time. Use a min-heap to track end times of ongoing meetings.
For each meeting, check if the earliest ending meeting finishes before this one starts. If so, reuse that room (pop from heap).
The heap size at any point is the number of rooms simultaneously in use. Track the maximum heap size.
Why it works: the heap always contains the end times of currently occupied rooms. If the earliest end time <= current start, that room is free.
Alternative: sweep line. Create events (+1 at start, -1 at end). Sort by time (end before start for ties). Track max concurrent.
Edge cases: no meetings (0 rooms), all meetings overlap (n rooms), no overlaps (1 room).
Optimal Approach
Step 1: Sort intervals by start time.
Step 2: Use a min-heap of end times.
Step 3: For each meeting [start, end]:
If heap[0] <= start, pop (room is free, reuse it).
Push end onto heap.
Step 4: Return len(heap) — the max rooms used.
Time: O(n log n) — sort + heap operations. Space: O(n).
Sort by start, keep end times in a min-heap
What Trips People Up in Real Interviews
Confusing this with Meeting Rooms I. That problem asks if all meetings can attend (no overlaps). This one asks for the minimum number of rooms.
Sorting by end time instead of start time. You need to process meetings in start-time order to correctly count concurrent meetings.
Not reusing rooms. When a meeting ends before the next one starts, that room is free. Pop from the heap.
Forgetting to track the maximum heap size. The answer is the maximum number of simultaneous meetings, not the final heap size.
Using a max-heap instead of a min-heap. A max-heap tracks the latest ending meeting — useless for room reuse. A min-heap gives you the earliest ending meeting to check availability.
Solution Code
O(n log n) timeimport heapq
def minMeetingRooms(intervals):
if not intervals:
return 0
intervals.sort(key=lambda x: x[0])
heap = []
for start, end in intervals:
if heap and heap[0] <= start:
heapq.heappop(heap)
heapq.heappush(heap, end)
return len(heap)Frequently Asked Questions
What is the Meeting Rooms II problem?
Given an array of meeting time intervals, find the minimum number of conference rooms required. This is a classic scheduling problem that tests heap and greedy skills.
How do you solve Meeting Rooms II?
The optimal approach runs in O(n log n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Meeting Rooms II?
Meeting Rooms II is asked at Google, Meta, Amazon, Microsoft, Apple, Netflix, Uber, Salesforce, TikTok, JPMorgan. It is a medium difficulty problem.
What are common mistakes on Meeting Rooms II?
- Confusing this with Meeting Rooms I. That problem asks if all meetings can attend (no overlaps). This one asks for the minimum number of rooms.
- Sorting by end time instead of start time. You need to process meetings in start-time order to correctly count concurrent meetings.
- Not reusing rooms. When a meeting ends before the next one starts, that room is free. Pop from the heap.
- Forgetting to track the maximum heap size. The answer is the maximum number of simultaneous meetings, not the final heap size.
- Using a `max-heap` instead of a `min-heap`. A `max-heap` tracks the latest ending meeting — useless for room reuse. A `min-heap` gives you the earliest ending meeting to check availability.