Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort intervals by their end time.
DSA Typescript
intervals.sort((a, b) => a[[1]] - b[[1]]);
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by start time instead of end time.
Using incorrect index for end time.
✗ Incorrect
We sort intervals by their end time, which is at index 1 in each interval array.
2fill in blank
mediumComplete the code to initialize the variable that tracks the end of the last added interval.
DSA Typescript
let end = intervals[0][[1]];
Attempts:
3 left
💡 Hint
Common Mistakes
Using start time instead of end time.
Using an invalid index.
✗ Incorrect
We initialize 'end' with the end time of the first interval, which is at index 1.
3fill in blank
hardFix the error in the condition that checks if the current interval overlaps with the last added interval.
DSA Typescript
if (intervals[i][0] < [1]) { count++; } else { end = intervals[i][1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing start with start or end with start incorrectly.
Using wrong variable in condition.
✗ Incorrect
We compare the start of the current interval with the end of the last added interval to detect overlap.
4fill in blank
hardFill both blanks to complete the loop that counts intervals to remove.
DSA Typescript
for (let [1] = 1; [1] < intervals.[2]; i++) { /* loop body */ }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong loop variable name.
Using incorrect property for array length.
✗ Incorrect
We use 'i' as the loop variable starting from 1, and 'length' to get the number of intervals.
5fill in blank
hardFill all three blanks to complete the function that returns the minimum number of intervals to remove.
DSA Typescript
function eraseOverlapIntervals(intervals: number[][]): number {
if (intervals.length === 0) return 0;
intervals.sort((a, b) => a[[1]] - b[[2]]);
let count = 0;
let end = intervals[0][[3]];
for (let i = 1; i < intervals.length; i++) {
if (intervals[i][0] < end) {
count++;
} else {
end = intervals[i][1];
}
}
return count;
}Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up start and end indices in sort.
Initializing 'end' with start time instead of end time.
✗ Incorrect
We sort intervals by their end time, which is at index 1 in each interval array for both a and b. Initialize 'end' with the end time of the first interval, which is at index 1.
