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 JavaScript code that calculates the minimum number of intervals to remove to avoid overlaps?
DSA Javascript
function eraseOverlapIntervals(intervals) {
if (intervals.length === 0) return 0;
intervals.sort((a, b) => a[1] - b[1]);
let count = 0;
let end = intervals[0][1];
for (let i = 1; i < intervals.length; i++) {
if (intervals[i][0] < end) {
count++;
} else {
end = intervals[i][1];
}
}
return count;
}
const intervals = [[1,3],[2,4],[3,5],[6,7]];
console.log(eraseOverlapIntervals(intervals));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 removal to avoid overlaps.
🧠 Conceptual
intermediateKey idea behind minimum removals in intervals
Which of the following best describes the key idea to find the minimum number of intervals to remove to avoid overlaps?
Attempts:
2 left
💡 Hint
Think about maximizing the number of intervals you keep without overlap.
✗ Incorrect
Sorting by end time and greedily choosing intervals that finish earliest allows keeping the maximum number of non-overlapping intervals, minimizing removals.
🔧 Debug
advancedIdentify the error in interval removal code
What error does the following code produce when run with intervals [[1,5],[2,3],[3,4]]?
function eraseOverlapIntervals(intervals) {
intervals.sort((a, b) => a[0] - b[0]);
let count = 0;
let end = intervals[0][1];
for (let i = 1; i < intervals.length; i++) {
if (intervals[i][0] < end) {
count++;
} else {
end = intervals[i][1];
}
}
return count;
}
console.log(eraseOverlapIntervals([[1,5],[2,3],[3,4]]));
Attempts:
2 left
💡 Hint
Check how sorting by start time affects overlap counting.
✗ Incorrect
Sorting by start time instead of end time causes the algorithm to count more removals than necessary. The correct approach is to sort by end time.
❓ Predict Output
advancedOutput after removing minimum overlapping intervals
What is the output of the following code that returns the intervals after removing the minimum number of overlapping intervals?
DSA Javascript
function eraseOverlapIntervals(intervals) {
if (intervals.length === 0) return [];
intervals.sort((a, b) => a[1] - b[1]);
const result = [intervals[0]];
let end = intervals[0][1];
for (let i = 1; i < intervals.length; i++) {
if (intervals[i][0] >= end) {
result.push(intervals[i]);
end = intervals[i][1];
}
}
return result;
}
const intervals = [[1,2],[2,3],[3,4],[1,3]];
console.log(eraseOverlapIntervals(intervals));Attempts:
2 left
💡 Hint
Check which intervals are kept after sorting by end time and skipping overlaps.
✗ Incorrect
The code keeps intervals that start after or at the end of the last chosen interval, resulting in the non-overlapping intervals [[1,2],[2,3],[3,4]].
🧠 Conceptual
expertWhy does sorting by end time minimize removals in intervals?
Why does sorting intervals by their end time lead to the minimum number of removals to avoid overlaps?
Attempts:
2 left
💡 Hint
Think about how choosing intervals that finish earliest affects the rest.
✗ Incorrect
Choosing intervals that end earliest leaves maximum space for the rest, allowing more intervals to fit without overlap, thus minimizing removals.
