Meeting Rooms Problem Minimum Rooms Required in DSA Javascript - Time & Space Complexity
We want to find out how the time needed to find the minimum number of meeting rooms changes as the number of meetings grows.
How does the code handle more meetings and what costs increase the most?
Analyze the time complexity of the following code snippet.
function minMeetingRooms(intervals) {
intervals.sort((a, b) => a[0] - b[0]);
const heap = [];
for (const interval of intervals) {
if (heap.length && heap[0] <= interval[0]) {
heap.shift();
}
heap.push(interval[1]);
heap.sort((a, b) => a - b);
}
return heap.length;
}
This code sorts meetings by start time, then uses a list to track end times of ongoing meetings to find the minimum rooms needed.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through all meetings once.
- How many times: Once for each meeting (n times).
- Inside loop: Sorting the heap array each time, which depends on the current heap size.
As the number of meetings grows, the sorting inside the loop grows too because the heap can grow up to n in size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 times sorting small arrays (up to 10 elements) |
| 100 | About 100 times sorting arrays that can grow up to 100 elements |
| 1000 | About 1000 times sorting arrays that can grow up to 1000 elements |
Pattern observation: The sorting inside the loop causes the total work to grow roughly with the cube of the input size.
Time Complexity: O(n^3 log n)
This means the time needed grows very quickly as the number of meetings increases, mainly because sorting happens inside the loop.
[X] Wrong: "Sorting once at the start means the whole code runs in O(n log n)."
[OK] Correct: Sorting inside the loop for each meeting adds extra cost, making the total time much larger than just the initial sort.
Understanding how nested sorting affects time helps you explain and improve solutions in interviews confidently.
"What if we used a priority queue (min-heap) instead of sorting the heap array each time? How would the time complexity change?"
Practice
Solution
Step 1: Understand the problem goal
The problem asks for the minimum number of rooms required so that no two meetings overlap in the same room.Step 2: Clarify what is being counted
We count how many meetings happen at the same time to find the minimum rooms needed.Final Answer:
The minimum number of meeting rooms needed so no meetings overlap -> Option AQuick Check:
Minimum rooms = max overlapping meetings [OK]
- Confusing total meetings with rooms needed
- Ignoring overlapping times
- Thinking earliest start time is the answer
Solution
Step 1: Identify sorting by start time
We want to sort intervals by their start property in ascending order.Step 2: Check the sort function
The comparator (a, b) => a.start - b.start sorts ascending by start time.Final Answer:
intervals.sort((a, b) => a.start - b.start); -> Option DQuick Check:
Ascending start time sort = intervals.sort((a, b) => a.start - b.start); [OK]
- Sorting by end time instead of start
- Sorting descending when ascending needed
- Using incorrect comparator syntax
const intervals = [{start: 0, end: 30}, {start: 5, end: 10}, {start: 15, end: 20}];Solution
Step 1: Analyze overlapping intervals
Meetings: [0-30], [5-10], [15-20]. The first overlaps with the second and third.Step 2: Count max overlaps
At time 5-10, two meetings overlap (0-30 and 5-10). At 15-20, two meetings overlap (0-30 and 15-20). Max overlap is 2.Final Answer:
2 -> Option BQuick Check:
Max overlapping meetings = 2 rooms needed [OK]
- Counting total meetings instead of overlaps
- Assuming 3 rooms because 3 meetings exist
- Ignoring overlap intervals
function minMeetingRooms(intervals) {
intervals.sort((a, b) => a.start - b.start);
let rooms = [];
for (let meeting of intervals) {
if (rooms.length === 0 || meeting.start < rooms[rooms.length - 1]) {
rooms.push(meeting.end);
} else {
rooms[rooms.length - 1] = meeting.end;
}
}
return rooms.length;
}Solution
Step 1: Understand rooms array usage
Rooms array stores end times of meetings currently occupying rooms.Step 2: Check comparison logic
Comparing meeting.start with rooms[rooms.length - 1] (last end time) is incorrect because rooms should be a min-heap or sorted structure, not just last element.Final Answer:
It compares meeting.start with a number incorrectly (rooms array stores end times, but comparison is wrong) -> Option CQuick Check:
Incorrect comparison of start with last end time causes bug [OK]
- Comparing start with last end time without sorting
- Not using a priority queue for room end times
- Returning wrong variable
[[1,4],[2,5],[7,9],[6,10],[8,11]], what is the minimum number of meeting rooms required? Explain your reasoning.Solution
Step 1: Sort intervals by start time
Sorted intervals: [1,4], [2,5], [6,10], [7,9], [8,11]Step 2: Track overlapping meetings
Between 1-5, two meetings overlap ([1,4] and [2,5]). Between 6-11, three meetings overlap ([6,10], [7,9], [8,11]). Max overlap is 3.Final Answer:
3 -> Option AQuick Check:
Max overlapping meetings = 3 rooms needed [OK]
- Not sorting intervals before checking overlaps
- Counting total meetings instead of max overlaps
- Missing overlaps in later intervals
Ready to prove it?
Pass the test and this pattern moves unknown
