Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a vector of intervals represented as tuples.
DSA Rust
let intervals = vec![(1, 3), (6, 9), (2, [1])];
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing an end value less than or equal to the start value.
Selecting a number that does not overlap with existing intervals.
✗ Incorrect
The interval (2, 4) fits the common example of overlapping intervals.
2fill in blank
mediumComplete the code to sort intervals by their start time.
DSA Rust
intervals.sort_by_key(|interval| interval.[1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0, which refers to the end time.
Using string keys which are invalid for tuple indexing.
✗ Incorrect
Tuples in Rust are indexed starting at 0, so interval.0 is the start time.
3fill in blank
hardFix the error in the code to merge overlapping intervals.
DSA Rust
if intervals[i].1 >= intervals[[1]].0 { merged.push((intervals[[1]].0, intervals[i].1)); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using i + 1 which may cause out-of-bound errors.
Using i which compares the same interval.
✗ Incorrect
We compare the current interval's end with the previous interval's start, so index i-1 is needed.
4fill in blank
hardFill both blanks to filter intervals that start after 5 and end before 10.
DSA Rust
let filtered: Vec<_> = intervals.iter().filter(|&&([1], [2])| [1] > 5 && [2] < 10).collect();
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid variable names that don't match the tuple structure.
Not destructuring the tuple properly.
✗ Incorrect
The closure destructures the tuple into variables s and e representing start and end.
5fill in blank
hardFill all three blanks to create a map from interval start to interval length for intervals longer than 2.
DSA Rust
let interval_map: std::collections::HashMap<_, _> = intervals.iter()
.filter(|&&(start, end)| end - start [1] 2)
.map(|&(start, end)| (start, end [2] start))
.collect::<std::collections::HashMap<_, _>>();Attempts:
3 left
💡 Hint
Common Mistakes
Using >= instead of > changes the filter condition.
Using + instead of - for length calculation.
✗ Incorrect
We filter intervals longer than 2 (end - start > 2) and map start to length (end - start).
