Challenge - 5 Problems
Interval Master
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 TypeScript code that calculates the minimum number of intervals to remove to avoid overlaps?
DSA Typescript
function eraseOverlapIntervals(intervals: number[][]): number {
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,2],[2,3],[3,4],[1,3]];
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. It then counts how many intervals overlap with the previous chosen interval. Here, only one interval needs to be removed to avoid overlap.
🧠 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 to avoid overlaps?
Attempts:
2 left
💡 Hint
Think about how choosing intervals that finish earlier affects the rest.
✗ Incorrect
Choosing intervals with earliest end time leaves maximum space for the next intervals, reducing overlaps and removals.
🔧 Debug
advancedIdentify the error in interval removal code
What error does the following TypeScript code produce when run, and why?
DSA Typescript
function eraseOverlapIntervals(intervals: number[][]): number {
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([]));Attempts:
2 left
💡 Hint
Check what happens when intervals array is empty.
✗ Incorrect
The code tries to access intervals[0][1] without checking if intervals is empty, causing a TypeError.
❓ Predict Output
advancedOutput of minimum removals with overlapping intervals
What is the output of this TypeScript code snippet?
DSA Typescript
function eraseOverlapIntervals(intervals: number[][]): number {
intervals.sort((a, b) => a[1] - b[1]);
let count = 0;
let end = -Infinity;
for (const interval of intervals) {
if (interval[0] >= end) {
end = interval[1];
} else {
count++;
}
}
return count;
}
const intervals = [[1,5],[2,3],[3,4],[4,6]];
console.log(eraseOverlapIntervals(intervals));Attempts:
2 left
💡 Hint
Count how many intervals overlap after sorting by end time.
✗ Incorrect
The code sorts intervals by end time and counts overlaps. Here, only one interval needs to be removed.
🚀 Application
expertMinimum removals for large interval set
Given the following intervals, what is the minimum number of intervals to remove to make the rest non-overlapping?
DSA Typescript
const intervals = [[0,30],[5,10],[15,20],[25,35],[27,40],[40,50],[45,55],[50,60],[55,65],[60,70]]; function eraseOverlapIntervals(intervals: number[][]): number { intervals.sort((a, b) => a[1] - b[1]); let count = 0; let end = -Infinity; for (const interval of intervals) { if (interval[0] >= end) { end = interval[1]; } else { count++; } } return count; } console.log(eraseOverlapIntervals(intervals));
Attempts:
2 left
💡 Hint
Sort by end time and count overlaps carefully.
✗ Incorrect
After sorting by end time, the maximum number of non-overlapping intervals is 6, so minimum removals are 4.
