Challenge - 5 Problems
Interval Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of minimum removals for overlapping intervals
What is the output of the following C++ code that calculates the minimum number of intervals to remove to avoid overlaps?
DSA C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; int eraseOverlapIntervals(vector<pair<int,int>>& intervals) { if(intervals.empty()) return 0; sort(intervals.begin(), intervals.end(), [](auto &a, auto &b){ return a.second < b.second; }); int count = 0; int end = intervals[0].second; for(int i = 1; i < intervals.size(); i++) { if(intervals[i].first < end) { count++; } else { end = intervals[i].second; } } return count; } int main() { vector<pair<int,int>> intervals = {{1,3},{2,4},{3,5},{6,7}}; cout << eraseOverlapIntervals(intervals) << endl; return 0; }
Attempts:
2 left
💡 Hint
Sort intervals by their end time and count overlaps.
✗ Incorrect
The code sorts intervals by their end time and counts how many intervals overlap with the previous chosen interval. Only one interval needs to be removed to avoid overlaps.
🧠 Conceptual
intermediateUnderstanding the greedy choice in interval removal
Why does sorting intervals by their end time help in minimizing the number of intervals to remove?
Attempts:
2 left
💡 Hint
Think about how choosing intervals affects the space left for others.
✗ Incorrect
Choosing intervals that end earliest allows more intervals to fit after them without overlap, minimizing removals.
🔧 Debug
advancedIdentify the error in interval removal code
What error does the following code produce when run with intervals {{1,4},{2,3},{3,5},{6,7}}?
int eraseOverlapIntervals(vector>& intervals) {
sort(intervals.begin(), intervals.end());
int count = 0;
int end = intervals[0].second;
for(int i = 1; i < intervals.size(); i++) {
if(intervals[i].first < end) {
count++;
} else {
end = intervals[i].second;
}
}
return count;
}
Attempts:
2 left
💡 Hint
Check how intervals are sorted and how end is updated.
✗ Incorrect
Sorting by default sorts by start time, not end time, so the greedy choice fails and count is incorrect.
❓ Predict Output
advancedOutput of minimum removals with nested intervals
What is the output of this code for intervals {{1,10},{2,3},{3,4},{4,5}}?
DSA C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; int eraseOverlapIntervals(vector<pair<int,int>>& intervals) { if(intervals.empty()) return 0; sort(intervals.begin(), intervals.end(), [](auto &a, auto &b){ return a.second < b.second; }); int count = 0; int end = intervals[0].second; for(int i = 1; i < intervals.size(); i++) { if(intervals[i].first < end) { count++; } else { end = intervals[i].second; } } return count; } int main() { vector<pair<int,int>> intervals = {{1,10},{2,3},{3,4},{4,5}}; cout << eraseOverlapIntervals(intervals) << endl; return 0; }
Attempts:
2 left
💡 Hint
The large interval overlaps with all smaller ones.
✗ Incorrect
The large interval {1,10} overlaps with all others, so it must be removed to avoid overlaps, resulting in 1 removal.
🚀 Application
expertMinimum removals for intervals with same end time
Given intervals {{1,5},{2,5},{3,5},{4,5}}, what is the minimum number of intervals to remove to avoid overlaps?
Attempts:
2 left
💡 Hint
All intervals end at the same time, so only one can remain.
✗ Incorrect
Since all intervals end at 5 and start at different times, they all overlap. Only one can stay, so 3 must be removed.
