📖 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 Java program that calculates the minimum number of meeting rooms required given a list of meeting time intervals.
📋 What You'll Learn
Create an array of meeting intervals with exact start and end times
Create a variable to count the minimum number of rooms needed
Write logic to find the minimum number of meeting rooms required
Print the minimum number of meeting rooms needed
💡 Why This Matters
🌍 Real World
Scheduling meetings efficiently in offices or conference centers to avoid conflicts.
💼 Career
Useful for software engineers working on calendar apps, event management systems, or resource scheduling.
Progress0 / 4 steps
1
Create the meeting intervals array
Create a 2D integer array called meetings with these exact intervals: {0, 30}, {5, 10}, and {15, 20}.
DSA Java
Hint
Use a 2D array to store start and end times of meetings.
2
Create a variable to track the minimum rooms needed
Create an integer variable called minRooms and set it to 0.
DSA Java
Hint
This variable will hold the minimum number of rooms needed.
3
Calculate the minimum number of meeting rooms required
Write code to calculate the minimum number of meeting rooms needed using the following steps: sort the meetings array by start time, create two arrays startTimes and endTimes from meetings, use two pointers startPointer and endPointer to traverse these arrays, and update minRooms to track the maximum number of overlapping meetings.
DSA Java
Hint
Use two pointers to compare start and end times and count overlapping meetings.
4
Print the minimum number of meeting rooms needed
Write a System.out.println statement to print the value of minRooms.
DSA Java
Hint
Print the variable minRooms to see the minimum rooms needed.
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 earliest meeting start time
B. The minimum number of meeting rooms needed so no meetings overlap
C. The total number of meetings scheduled
D. The maximum length of a single meeting
Solution
Step 1: Understand the problem goal
The problem asks for the minimum number of rooms needed so that no two meetings overlap in the same room.
Step 2: Clarify what is being counted
We count rooms, not meetings or times. The goal is to avoid overlap in any single room.
Final Answer:
The minimum number of meeting rooms needed so no meetings overlap -> Option B
Quick Check:
Minimum rooms = no overlapping meetings [OK]
Hint: Count overlaps to find minimum rooms needed [OK]
Common Mistakes:
Confusing number of meetings with number of rooms
Thinking about meeting length instead of overlaps
Ignoring overlapping intervals
2. Which Java data structure is best to track ongoing meetings by their end times when solving the Meeting Rooms problem?
easy
A. HashMap
B. ArrayList
C. Stack
D. PriorityQueue (Min-Heap)
Solution
Step 1: Identify the need to track earliest ending meeting
We need to quickly find the meeting that ends earliest to reuse its room if possible.
Step 2: Choose data structure supporting quick min extraction
A PriorityQueue (Min-Heap) efficiently gives the earliest end time meeting.
Final Answer:
PriorityQueue (Min-Heap) -> Option D
Quick Check:
Min-heap tracks earliest end time [OK]
Hint: Use min-heap to track earliest meeting end [OK]
Common Mistakes:
Using HashMap which doesn't order by end time
Using Stack which is LIFO, not suitable here
Using ArrayList without sorting or heap
3. What is the output of the following Java code snippet that calculates minimum meeting rooms needed?
int[][] intervals = {{0, 30}, {5, 10}, {15, 20}};
System.out.println(minMeetingRooms(intervals));
public static int minMeetingRooms(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
PriorityQueue<Integer> heap = new PriorityQueue<>();
for (int[] interval : intervals) {
if (!heap.isEmpty() && heap.peek() <= interval[0]) {
heap.poll();
}
heap.add(interval[1]);
}
return heap.size();
}
medium
A. 1
B. 3
C. 2
D. 0
Solution
Step 1: Sort intervals by start time
Sorted intervals: [0,30], [5,10], [15,20]
Step 2: Use min-heap to track end times and count rooms
- Add 30 to heap
- Next interval starts at 5, 5 < 30, add 10 to heap (2 rooms)
- Next interval starts at 15, 15 > 10, remove 10, add 20 (still 2 rooms)
Heap size = 2
Final Answer:
2 -> Option C
Quick Check:
Overlaps require 2 rooms [OK]
Hint: Sort by start, use min-heap for end times [OK]
Common Mistakes:
Not sorting intervals before processing
Not removing ended meetings from heap
Counting total meetings instead of rooms
4. Find the bug in this Java code for minimum meeting rooms:
public static int minMeetingRooms(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> a[1] - b[1]);
PriorityQueue<Integer> heap = new PriorityQueue<>();
for (int[] interval : intervals) {
if (!heap.isEmpty() && heap.peek() <= interval[0]) {
heap.poll();
}
heap.add(interval[1]);
}
return heap.size();
}
medium
A. Sorting intervals by end time instead of start time
B. Using PriorityQueue instead of Stack
C. Not checking if heap is empty before peek
D. Adding interval start time instead of end time to heap
Solution
Step 1: Check sorting criteria
The code sorts intervals by end time, but we must sort by start time to process meetings in order.
Step 2: Understand impact of wrong sorting
Sorting by end time breaks the logic of checking overlaps correctly, causing wrong room count.
Final Answer:
Sorting intervals by end time instead of start time -> Option A
Quick Check:
Sort by start time, not end time [OK]
Hint: Sort intervals by start time before processing [OK]
Common Mistakes:
Sorting by end time instead of start time
Ignoring heap empty check (though code does it here)
Adding wrong values to heap
5. Given these meeting intervals: [[1,4],[2,5],[7,9],[6,8],[9,10]], what is the minimum number of meeting rooms required?