Challenge - 5 Problems
Interval Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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);
Attempts:
2 left
💡 Hint
Think about how many intervals can fit without overlapping after sorting by end time.
✗ Incorrect
The code sorts intervals by their end time and greedily counts the maximum number of non-overlapping intervals. The minimum removals is total intervals minus this count. Here, 3 intervals can be non-overlapping, so 1 must be removed.
🧠 Conceptual
intermediate1: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?
Attempts:
2 left
💡 Hint
Think about how the end time affects the possibility of fitting more intervals after it.
✗ Incorrect
Choosing intervals that end earliest allows more intervals to fit after them without overlapping, minimizing removals.
🔧 Debug
advanced1: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;
}
}
}
Attempts:
2 left
💡 Hint
Check carefully how the swap is done for both start and end times.
✗ Incorrect
The code correctly swaps both start and end times of intervals to sort by end time using bubble sort.
❓ Predict Output
advanced2: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);
Attempts:
2 left
💡 Hint
Count how many intervals can be chosen without overlap after sorting by end time.
✗ Incorrect
The code sorts intervals by their end time and greedily counts the maximum number of non-overlapping intervals. The minimum removals is total intervals minus this count. Here, 3 intervals can be non-overlapping, so 1 must be removed.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about how the greedy choice leaves room for future intervals.
✗ Incorrect
Choosing intervals with earliest end time leaves the maximum room for subsequent intervals, ensuring the maximum number of intervals remain without overlap, thus minimizing removals.
