By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
"This problem appears in 1 out of every 3 onsite interviews—master it, and you’ll prove you can handle real-world scheduling constraints with optimal efficiency."
Before tackling this problem, ensure you understand: 1. Sorting (custom comparators, stability) 2. Min-Heap (Priority Queue) (insertion, extraction, peek) 3. Two-Pointer Technique (for merging intervals)
Follow these steps every time you see a "minimum platforms" or "meeting rooms" problem:
[start, end]
[start, duration]
Ask if end time is inclusive/exclusive (usually exclusive).
Choose the Right Technique
If follow-up asks for "which rooms" → Track room assignments (use heap of room IDs).
Sort Intervals (If Needed)
If using two-pointer, sort start and end times separately.
Initialize Data Structures
Two-Pointer: i (start pointer), j (end pointer), count = 0, max_count = 0.
i
j
count = 0
max_count = 0
Process Intervals
Two-Pointer Approach:
start[i] < end[j]
max_count
Edge Cases
0
1
All meetings at the same time → return n.
n
Optimize & Verify
O(n log n)
O(n)
O(1)
[ [1, 2], [2, 3] ]
[ [1, 5], [2, 3], [4, 6] ]
2
Problem: Given an array of meeting intervals [start, end], find the minimum number of rooms required.
Input: intervals = [[0, 30], [5, 10], [15, 20]] Output: 2
intervals = [[0, 30], [5, 10], [15, 20]]
[[0, 30], [5, 10], [15, 20]]
[0, 30]
30
[30]
[5, 10]
5
10
[10, 30]
[15, 20]
15
20
[20, 30]
import heapq def minMeetingRooms(intervals): if not intervals: return 0 # Sort by start time intervals.sort(key=lambda x: x[0]) # Min-heap to track end times heap = [] heapq.heappush(heap, intervals[0][1]) for i in range(1, len(intervals)): start, end = intervals[i] # Reuse the earliest ending room if heap[0] <= start: heapq.heappop(heap) heapq.heappush(heap, end) return len(heap)
Problem: Same as above, but intervals are already sorted.
starts = [0, 5, 15]
ends = [10, 20, 30]
i = 0
j = 0
i < n
j < n
starts[i] < ends[j]
count++
i++
0 < 10
count = 1
i = 1
5 < 10
count = 2
i = 2
count--
j++
15 > 10
j = 1
15 < 20
i = 3
max_count = max(max_count, count)
max_count = 2
def minMeetingRooms(intervals): if not intervals: return 0 starts = sorted([i[0] for i in intervals]) ends = sorted([i[1] for i in intervals]) i = j = count = max_count = 0 while i < len(intervals): if starts[i] < ends[j]: count += 1 i += 1 max_count = max(max_count, count) else: count -= 1 j += 1 return max_count
starts
ends
Problem: Return which rooms are assigned to which meetings (not just the count).
Input: intervals = [[0, 30], [5, 10], [15, 20]] Output: [[0, 30], [5, 10]], [[15, 20]] (Room 1: [0,30] and [15,20], Room 2: [5,10])
[[0, 30], [5, 10]], [[15, 20]]
[0,30]
[15,20]
[5,10]
(end_time, room_id)
import heapq def assignMeetingRooms(intervals): if not intervals: return [] intervals.sort(key=lambda x: x[0]) heap = [] room_assignments = {} room_id = 0 for start, end in intervals: if heap and heap[0][0] <= start: # Reuse the room _, reused_room = heapq.heappop(heap) room_assignments[reused_room].append([start, end]) heapq.heappush(heap, (end, reused_room)) else: # Assign a new room room_assignments[room_id] = [[start, end]] heapq.heappush(heap, (end, room_id)) room_id += 1 return list(room_assignments.values())
n = 0
n = 1
<=
<
count
(end_time, cost, room_id)
"Alright, let’s lock this in. When you see ‘Meeting Rooms II’ or ‘Minimum Platforms’: 1. Sort intervals by start time—always. 2. Use a min-heap to track end times. If the earliest ending meeting ends before the next starts, reuse the room. Otherwise, allocate a new one. 3. Two-pointer is faster if input is sorted—compare start and end times to count overlaps. 4. Edge cases: Empty input? Single meeting? All meetings at once? Handle them. 5. Follow-ups: Track room assignments? Use a heap with room IDs. Priorities? Switch to a max-heap.
You’ve got this. Walk in, sort, heap, and crush it."
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.