Bird
Raised Fist0
DSA Cprogramming

Meeting Rooms Problem Minimum Rooms Required in DSA C

Choose your learning style10 modes available
Mental Model
We want to find the smallest number of rooms needed so that no meetings overlap in the same room.
Analogy: Imagine you have several friends who want to use a few study rooms. Each friend has a start and end time. You want to know how many rooms you need so that no two friends share a room at the same time.
Meetings: [1,4], [2,5], [7,9]
Timeline: 1 2 3 4 5 6 7 8 9
Rooms needed:
Room1: [1,4]       [7,9]
Room2:    [2,5]
Dry Run Walkthrough
Input: meetings: [1,4], [2,5], [7,9]
Goal: Find the minimum number of rooms needed so no meetings overlap in the same room.
Step 1: Sort meetings by start time
[1,4], [2,5], [7,9]
Why: Sorting helps us process meetings in order of when they start.
Step 2: Create a min-heap to track meeting end times, add first meeting end time 4
Heap: [4]
Why: We track earliest finishing meeting to reuse rooms.
Step 3: Check second meeting start 2 against earliest end 4 in heap
Heap: [4,5]
Why: 2 < 4 means overlap, need new room, add end time 5.
Step 4: Check third meeting start 7 against earliest end 4 in heap
Heap after removing 4: [5]
Add 9: [5,9]
Why: 7 >= 4 means room freed, remove 4, add 9.
Result:
Heap size is 2, so minimum rooms needed = 2
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

// Structure for a meeting interval
typedef struct {
    int start;
    int end;
} Interval;

// Compare function for qsort to sort intervals by start time
int compare(const void *a, const void *b) {
    Interval *i1 = (Interval *)a;
    Interval *i2 = (Interval *)b;
    return i1->start - i2->start;
}

// Min-heap structure for end times
typedef struct {
    int *arr;
    int size;
    int capacity;
} MinHeap;

MinHeap* createHeap(int capacity) {
    MinHeap *heap = (MinHeap *)malloc(sizeof(MinHeap));
    heap->arr = (int *)malloc(sizeof(int) * capacity);
    heap->size = 0;
    heap->capacity = capacity;
    return heap;
}

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

void heapifyUp(MinHeap *heap, int index) {
    while (index > 0) {
        int parent = (index - 1) / 2;
        if (heap->arr[parent] <= heap->arr[index])
            break;
        swap(&heap->arr[parent], &heap->arr[index]);
        index = parent;
    }
}

void heapifyDown(MinHeap *heap, int index) {
    int left, right, smallest;
    while (1) {
        left = 2 * index + 1;
        right = 2 * index + 2;
        smallest = index;
        if (left < heap->size && heap->arr[left] < heap->arr[smallest])
            smallest = left;
        if (right < heap->size && heap->arr[right] < heap->arr[smallest])
            smallest = right;
        if (smallest == index)
            break;
        swap(&heap->arr[smallest], &heap->arr[index]);
        index = smallest;
    }
}

void insertHeap(MinHeap *heap, int val) {
    if (heap->size == heap->capacity) return; // no resize for simplicity
    heap->arr[heap->size] = val;
    heapifyUp(heap, heap->size);
    heap->size++;
}

int getMin(MinHeap *heap) {
    if (heap->size == 0) return -1;
    return heap->arr[0];
}

void removeMin(MinHeap *heap) {
    if (heap->size == 0) return;
    heap->arr[0] = heap->arr[heap->size - 1];
    heap->size--;
    heapifyDown(heap, 0);
}

int minMeetingRooms(Interval* intervals, int intervalsSize) {
    if (intervalsSize == 0) return 0;

    // Sort intervals by start time
    qsort(intervals, intervalsSize, sizeof(Interval), compare);

    // Create min heap for end times
    MinHeap *heap = createHeap(intervalsSize);

    // Add first meeting end time
    insertHeap(heap, intervals[0].end);

    for (int i = 1; i < intervalsSize; i++) {
        int earliestEnd = getMin(heap);
        if (intervals[i].start >= earliestEnd) {
            // Room freed, remove earliest end
            removeMin(heap);
        }
        // Add current meeting end time
        insertHeap(heap, intervals[i].end);
    }

    int rooms = heap->size;
    free(heap->arr);
    free(heap);
    return rooms;
}

int main() {
    Interval meetings[] = {{1,4}, {2,5}, {7,9}};
    int n = sizeof(meetings) / sizeof(meetings[0]);
    int result = minMeetingRooms(meetings, n);
    printf("Minimum rooms needed: %d\n", result);
    return 0;
}
qsort(intervals, intervalsSize, sizeof(Interval), compare);
Sort meetings by start time to process in order
insertHeap(heap, intervals[0].end);
Add first meeting end time to heap
int earliestEnd = getMin(heap);
Get earliest finishing meeting end time
if (intervals[i].start >= earliestEnd) { removeMin(heap); }
If current meeting starts after earliest end, reuse room by removing earliest end
insertHeap(heap, intervals[i].end);
Add current meeting end time to heap
OutputSuccess
Minimum rooms needed: 2
Complexity Analysis
Time: O(n log n) because we sort the meetings and then insert/remove from a heap for each meeting
Space: O(n) for the heap that stores end times of ongoing meetings
vs Alternative: Naive approach checks all pairs for overlap O(n^2), this heap method is more efficient
Edge Cases
No meetings
Returns 0 rooms needed
DSA C
if (intervalsSize == 0) return 0;
All meetings non-overlapping
Returns 1 room needed
DSA C
if (intervals[i].start >= earliestEnd) { removeMin(heap); }
All meetings fully overlapping
Returns number of meetings as rooms needed
DSA C
insertHeap(heap, intervals[i].end);
When to Use This Pattern
When you need to find the minimum number of resources (rooms) to schedule overlapping intervals, use a min-heap to track earliest finishing times and allocate rooms efficiently.
Common Mistakes
Mistake: Not sorting meetings by start time before processing
Fix: Sort intervals by start time first to process in chronological order
Mistake: Not removing earliest ending meeting when room is freed
Fix: Remove earliest end from heap when current meeting can reuse that room
Summary
Finds the minimum number of rooms needed so no meetings overlap in the same room.
Use when scheduling intervals to minimize resource usage like rooms or machines.
Sort meetings by start time and use a min-heap to track earliest finishing meeting to reuse rooms.

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 to hold all meetings without overlap
B. The maximum duration of a single meeting
C. The total number of meetings scheduled
D. The earliest start time among all meetings

Solution

  1. 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.
  2. Step 2: Clarify what is asked

    We want to find how many rooms are needed to schedule all meetings without time conflicts.
  3. Final Answer:

    The minimum number of meeting rooms needed to hold all meetings without overlap -> Option A
  4. Quick Check:

    Minimum rooms = no overlapping meetings at the same time [OK]
Hint: Count max overlapping meetings to find minimum rooms [OK]
Common Mistakes:
  • Confusing total meetings with rooms needed
  • Thinking about meeting durations instead of overlaps
  • Ignoring overlapping intervals
2. Which of the following C code snippets correctly declares an array of meeting intervals where each interval has a start and end time?
easy
A. int intervals[2][3] = {{0, 5, 15}, {30, 10, 20}};
B. int intervals[3] = {0, 30, 5, 10, 15, 20};
C. int intervals[3][2] = {0, 30, 5, 10, 15, 20};
D. int intervals[3][2] = {{0, 30}, {5, 10}, {15, 20}};

Solution

  1. Step 1: Check array dimensions and initialization

    We need a 2D array with 3 intervals, each having 2 values (start and end).
  2. Step 2: Validate each option

    int intervals[3][2] = {{0, 30}, {5, 10}, {15, 20}}; correctly declares int intervals[3][2] with proper nested braces for each interval.
  3. Final Answer:

    int intervals[3][2] = {{0, 30}, {5, 10}, {15, 20}}; -> Option D
  4. Quick Check:

    Correct 2D array syntax for intervals [OK]
Hint: Use int arr[n][2] for intervals with start and end [OK]
Common Mistakes:
  • Using 1D array for 2D interval data
  • Incorrect brace nesting in initialization
  • Swapping rows and columns in declaration
3. Given the following C code snippet, what is the output printed?
int intervals[3][2] = {{0, 30}, {5, 10}, {15, 20}};
// Assume a function minMeetingRooms(intervals, 3) returns minimum rooms needed
int rooms = minMeetingRooms(intervals, 3);
printf("%d\n", rooms);
Assuming minMeetingRooms correctly implements the meeting rooms logic, what is printed?
medium
A. 1
B. 3
C. 2
D. 0

Solution

  1. Step 1: Analyze meeting intervals for overlaps

    Intervals: [0,30], [5,10], [15,20]. The first and second overlap (5 < 30), so need at least 2 rooms.
  2. Step 2: Check if any three overlap simultaneously

    Third interval [15,20] overlaps with first [0,30], but second ends at 10, so max overlap is 2 meetings at once.
  3. Final Answer:

    2 -> Option C
  4. Quick Check:

    Max overlapping meetings = 2 [OK]
Hint: Count max overlaps to find rooms needed [OK]
Common Mistakes:
  • Assuming all meetings overlap simultaneously
  • Counting total meetings instead of overlaps
  • Ignoring partial overlaps
4. The following C function aims to find the minimum number of meeting rooms required but has a bug. What is the bug?
int minMeetingRooms(int intervals[][2], int n) {
    int rooms = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (intervals[i][0] < intervals[j][1] && intervals[j][0] < intervals[i][1]) {
                rooms++;
            }
        }
    }
    return rooms;
}
medium
A. It counts overlapping pairs but does not track maximum overlaps for rooms
B. It uses wrong indices in the loops causing out-of-bounds access
C. It returns zero always because rooms is never incremented
D. It sorts intervals incorrectly before counting overlaps

Solution

  1. Step 1: Understand the nested loops and condition

    The code counts each overlapping pair by checking if intervals overlap and increments rooms.
  2. Step 2: Identify why counting pairs is incorrect

    Counting pairs does not give the minimum rooms needed; we need the maximum number of simultaneous overlaps.
  3. Final Answer:

    It counts overlapping pairs but does not track maximum overlaps for rooms -> Option A
  4. Quick Check:

    Counting pairs ≠ max simultaneous overlaps [OK]
Hint: Track max overlaps, not just count pairs [OK]
Common Mistakes:
  • Confusing pair counts with room count
  • Not sorting intervals before processing
  • Ignoring simultaneous overlaps
5. You have the following meeting intervals: [[1, 4], [2, 5], [7, 9], [3, 6], [10, 11]]. Using the minimum rooms required approach, what is the minimum number of meeting rooms needed?
hard
A. 2
B. 3
C. 4
D. 5

Solution

  1. Step 1: Sort intervals by start time

    Sorted intervals: [1,4], [2,5], [3,6], [7,9], [10,11]
  2. Step 2: Find maximum overlapping meetings

    Between 1-6, three meetings overlap: [1,4], [2,5], [3,6]. After 6, meetings do not overlap.
  3. Final Answer:

    3 -> Option B
  4. Quick Check:

    Max overlaps = 3 rooms needed [OK]
Hint: Sort intervals, count max overlaps for rooms [OK]
Common Mistakes:
  • Not sorting intervals before counting
  • Counting total meetings instead of overlaps
  • Missing overlapping intervals in middle
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard