📖 Scenario: You are organizing meetings in a company. Each meeting has a start time and an end time. You want to find out the minimum number of meeting rooms needed so that all meetings can happen without overlapping in the same room.
🎯 Goal: Build a program that calculates the minimum number of meeting rooms required given a list of meeting time intervals.
📋 What You'll Learn
Create an array called meetings with exact meeting intervals as arrays of two numbers: [0, 30], [5, 10], and [15, 20]
Create a variable called rooms initialized to 0 to count the number of rooms needed
Write a function called minMeetingRooms that takes the meetings array and returns the minimum number of rooms required
Print the result of calling minMeetingRooms(meetings)
💡 Why This Matters
🌍 Real World
Scheduling meetings in offices or conference centers to avoid conflicts and optimize room usage.
💼 Career
Useful for software engineers working on calendar apps, event planners, and operations managers.
Progress0 / 4 steps
1
Create the meetings array
Create an array called meetings with these exact intervals: [0, 30], [5, 10], and [15, 20]
DSA Javascript
Hint
Use square brackets to create an array of arrays. Each inner array has two numbers for start and end times.
2
Create a variable to count rooms
Create a variable called rooms and set it to 0 to count the number of meeting rooms needed
DSA Javascript
Hint
Use let to create a variable that can change later.
3
Write the function to find minimum rooms
Write a function called minMeetingRooms that takes meetings as input and returns the minimum number of rooms needed. Use these steps inside the function: 1. Create two arrays starts and ends containing start times and end times of meetings respectively. 2. Sort both starts and ends arrays. 3. Use two pointers startPointer and endPointer starting at 0. 4. Loop while startPointer is less than the length of starts. 5. If starts[startPointer] is less than ends[endPointer], increment rooms and startPointer. 6. Else, decrement rooms, increment endPointer, and increment startPointer. 7. Return the maximum number of rooms needed during the process.
DSA Javascript
Hint
Use map to get start and end times. Sort them. Use two pointers to compare starts and ends. Increase rooms when a meeting starts before another ends. Decrease rooms when a meeting ends.
4
Print the minimum number of meeting rooms
Print the result of calling minMeetingRooms(meetings) to show the minimum number of meeting rooms required
DSA Javascript
Hint
Use console.log(minMeetingRooms(meetings)) to print the result.
Practice
(1/5)
1. What does the "Meeting Rooms Problem Minimum Rooms Required" ask you to find given a list of meeting time intervals?
easy
A. The minimum number of meeting rooms needed so no meetings overlap
B. The maximum length of a single meeting
C. The total number of meetings scheduled
D. The earliest meeting start time
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 A
Quick Check:
Minimum rooms = max overlapping meetings [OK]
Hint: Count max overlapping intervals to find rooms needed [OK]
Common Mistakes:
Confusing total meetings with rooms needed
Ignoring overlapping times
Thinking earliest start time is the answer
2. Which of the following JavaScript code snippets correctly sorts an array of meeting intervals by their start time? Assume intervals is an array of objects with 'start' and 'end' properties.
easy
A. intervals.sort((a, b) => b.end - a.end);
B. intervals.sort((a, b) => b.start - a.start);
C. intervals.sort((a, b) => a.end - b.end);
D. intervals.sort((a, b) => a.start - b.start);
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 D
Quick Check:
Ascending start time sort = intervals.sort((a, b) => a.start - b.start); [OK]
3. What is the minimum number of meeting rooms required for the following intervals? const intervals = [{start: 0, end: 30}, {start: 5, end: 10}, {start: 15, end: 20}];
medium
A. 1
B. 2
C. 3
D. 4
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 B
Quick Check:
Max overlapping meetings = 2 rooms needed [OK]
Hint: Find max overlaps by checking intervals start and end times [OK]
Common Mistakes:
Counting total meetings instead of overlaps
Assuming 3 rooms because 3 meetings exist
Ignoring overlap intervals
4. The following code attempts to find the minimum number of meeting rooms needed but has a bug. What is the bug?
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;
}
medium
A. It returns the wrong value (should return rooms array)
B. It sorts intervals by end time instead of start time
C. It compares meeting.start with a number incorrectly (rooms array stores end times, but comparison is wrong)
D. It uses push instead of unshift on rooms array
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 C
Quick Check:
Incorrect comparison of start with last end time causes bug [OK]
Hint: Use a min-heap or sorted structure for end times, not last element [OK]
Common Mistakes:
Comparing start with last end time without sorting
Not using a priority queue for room end times
Returning wrong variable
5. Given the intervals [[1,4],[2,5],[7,9],[6,10],[8,11]], what is the minimum number of meeting rooms required? Explain your reasoning.