Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new interval struct.
DSA Rust
struct Interval {
start: i32,
end: i32,
}
let new_interval = Interval { start: [1], end: 5 };Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names instead of a number for start.
✗ Incorrect
The new interval starts at 3 and ends at 5, so start must be 3.
2fill in blank
mediumComplete the code to push the new interval into the vector.
DSA Rust
let mut intervals = vec![Interval { start: 1, end: 2 }, Interval { start: 6, end: 9 }];
intervals.[1](new_interval);Attempts:
3 left
💡 Hint
Common Mistakes
Using insert without index.
Using append which requires another vector.
✗ Incorrect
To add a single element at the end of a vector, use push.
3fill in blank
hardFix the error in the loop that merges overlapping intervals.
DSA Rust
for i in 1..intervals.len() { if intervals[i].start [1] intervals[i - 1].end { intervals[i - 1].end = intervals[i].end; intervals.remove(i); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or < which misses overlapping intervals.
✗ Incorrect
We merge intervals if the current start is less than or equal to the previous end.
4fill in blank
hardFill both blanks to correctly insert the new interval in sorted order.
DSA Rust
let mut i = 0; while i < intervals.len() && intervals[i].start [1] new_interval.start { i [2] i + 1; } intervals.insert(i, new_interval);
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= causes wrong insertion point.
Using = instead of += for increment.
✗ Incorrect
We move forward while interval start is less than new start, and increment i by 1.
5fill in blank
hardFill all three blanks to create a merged list of intervals after insertion.
DSA Rust
let mut merged = Vec::new(); for interval in intervals { if merged.is_empty() || merged.last().unwrap().end [1] interval.start { merged.[2](interval); } else { merged.last_mut().unwrap().end = merged.last().unwrap().end.[3] interval.end; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= causes wrong merge logic.
Using min instead of max for merging.
✗ Incorrect
We add interval if no overlap (last end < current start), else merge by max end.
