Bird
0
0
DSA Cprogramming~20 mins

Non Overlapping Intervals Minimum Removal in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interval Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of minimum removals for overlapping intervals
What is the output of the following code that calculates the minimum number of intervals to remove to avoid overlaps?
DSA C
int intervals[][2] = {{1,3},{2,4},{3,5},{6,7}};
int n = 4;

// Sort intervals by end time
for(int i=0; i<n-1; i++) {
    for(int j=0; j<n-i-1; j++) {
        if(intervals[j][1] > intervals[j+1][1]) {
            int temp0 = intervals[j][0];
            int temp1 = intervals[j][1];
            intervals[j][0] = intervals[j+1][0];
            intervals[j][1] = intervals[j+1][1];
            intervals[j+1][0] = temp0;
            intervals[j+1][1] = temp1;
        }
    }
}

int count = 1;
int end = intervals[0][1];
for(int i=1; i<n; i++) {
    if(intervals[i][0] >= end) {
        count++;
        end = intervals[i][1];
    }
}

int result = n - count;
printf("%d\n", result);
A1
B2
C3
D0
Attempts:
2 left
💡 Hint
Think about how many intervals can fit without overlapping after sorting by end time.
🧠 Conceptual
intermediate
1:30remaining
Understanding the greedy choice in interval removal
Why does sorting intervals by their end time help in minimizing the number of intervals to remove to avoid overlaps?
ABecause intervals with earliest start time always overlap less.
BBecause sorting by length of intervals guarantees minimal removals.
CBecause sorting by start time ensures maximum intervals are removed.
DBecause choosing intervals with earliest end time leaves more room for following intervals.
Attempts:
2 left
💡 Hint
Think about how the end time affects the possibility of fitting more intervals after it.
🔧 Debug
advanced
1:30remaining
Identify the bug in interval sorting code
What is the bug in the following code snippet that sorts intervals by their end time? for(int i=0; i intervals[j+1][1]) { int temp0 = intervals[j][0]; int temp1 = intervals[j][1]; intervals[j][0] = intervals[j+1][0]; intervals[j][1] = intervals[j+1][1]; intervals[j+1][0] = temp0; intervals[j+1][1] = temp1; } } }
ASwapping intervals incorrectly swaps only end times, not start times.
BThe inner loop should run until j < n-i, not n-i-1.
CNo bug, the code correctly sorts intervals by end time.
DThe swap logic is correct but inefficient; no bug affects correctness.
Attempts:
2 left
💡 Hint
Check carefully how the swap is done for both start and end times.
Predict Output
advanced
2:00remaining
Output of minimum removals with overlapping intervals
What is the output of the following code snippet?
DSA C
int intervals[][2] = {{1,2},{2,3},{3,4},{1,3}};
int n = 4;

// Sort intervals by end time
for(int i=0; i<n-1; i++) {
    for(int j=0; j<n-i-1; j++) {
        if(intervals[j][1] > intervals[j+1][1]) {
            int temp0 = intervals[j][0];
            int temp1 = intervals[j][1];
            intervals[j][0] = intervals[j+1][0];
            intervals[j][1] = intervals[j+1][1];
            intervals[j+1][0] = temp0;
            intervals[j+1][1] = temp1;
        }
    }
}

int count = 1;
int end = intervals[0][1];
for(int i=1; i<n; i++) {
    if(intervals[i][0] >= end) {
        count++;
        end = intervals[i][1];
    }
}

int result = n - count;
printf("%d\n", result);
A1
B2
C3
D0
Attempts:
2 left
💡 Hint
Count how many intervals can be chosen without overlap after sorting by end time.
🧠 Conceptual
expert
2:30remaining
Why greedy approach works for minimum interval removals
Which statement best explains why the greedy approach of selecting intervals by earliest end time guarantees the minimum number of removals to avoid overlaps?
ABecause it tries all possible combinations of intervals to find the minimum removals.
BBecause selecting intervals with earliest end time always leads to the globally optimal solution by maximizing remaining space.
CBecause it removes the longest intervals first to minimize overlap.
DBecause it sorts intervals by start time and removes those that start too early.
Attempts:
2 left
💡 Hint
Think about how the greedy choice leaves room for future intervals.