Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort intervals by their end time.
DSA Rust
intervals.sort_by_key(|interval| interval.[1]);Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by start time instead of end time.
Sorting by interval length which does not guarantee minimal removals.
✗ Incorrect
Sorting intervals by their end time helps to greedily select intervals that finish earliest, minimizing overlaps.
2fill in blank
mediumComplete the code to initialize the count of removals to zero.
DSA Rust
let mut removals = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Starting removals at 1 which incorrectly assumes one interval is removed initially.
Starting removals at intervals.len() which is too large.
✗ Incorrect
We start with zero removals because initially we haven't removed any intervals.
3fill in blank
hardFix the error in the condition to check if current interval overlaps with previous.
DSA Rust
if intervals[i].[1] <= prev_end { removals += 1; } else { prev_end = intervals[i].end; }
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing current end with previous end instead of current start.
Using length or mid which are not relevant for overlap check.
✗ Incorrect
We check if the start of the current interval is less than or equal to the end of the previous to detect overlap.
4fill in blank
hardFill both blanks to update the previous end and increment removals correctly.
DSA Rust
if intervals[i].start <= prev_end { removals [1] 1; } else { prev_end [2] intervals[i].end; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-=' or '*=' which incorrectly changes removals or prev_end.
Using '=' to increment removals instead of '+='.
✗ Incorrect
We add 1 to removals when overlap is found and update prev_end to current interval's end when no overlap.
5fill in blank
hardFill all three blanks to complete the function that returns minimum removals.
DSA Rust
fn erase_overlap_intervals(mut intervals: Vec<Interval>) -> i32 {
if intervals.is_empty() { return 0; }
intervals.sort_by_key(|interval| interval.[1]);
let mut removals = [2];
let mut prev_end = intervals[0].end;
for i in 1..intervals.len() {
if intervals[i].start <= prev_end {
removals [3] 1;
} else {
prev_end = intervals[i].end;
}
}
removals
}Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by start instead of end in this implementation.
Initializing removals incorrectly.
Using '=' instead of '+=' to increment removals.
✗ Incorrect
Sort by end, initialize removals to 0, and increment removals with '+=' when overlap is found.
