Challenge - 5 Problems
Interval Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Merging Overlapping Intervals
What is the output of the following Rust code that merges overlapping intervals?
DSA Rust
fn merge_intervals(mut intervals: Vec<(i32, i32)>) -> Vec<(i32, i32)> {
intervals.sort_by_key(|k| k.0);
let mut merged = Vec::new();
for interval in intervals {
if let Some(last) = merged.last_mut() {
if interval.0 <= last.1 {
if interval.1 > last.1 {
last.1 = interval.1;
}
} else {
merged.push(interval);
}
} else {
merged.push(interval);
}
}
merged
}
fn main() {
let intervals = vec![(1, 3), (2, 6), (8, 10), (15, 18)];
let result = merge_intervals(intervals);
for (start, end) in result {
print!("({} -> {}) ", start, end);
}
}Attempts:
2 left
💡 Hint
Think about how overlapping intervals are combined into one continuous interval.
✗ Incorrect
The code sorts intervals by start time, then merges overlapping ones by extending the end time of the last merged interval. Intervals (1,3) and (2,6) overlap, so they merge into (1,6). Others remain separate.
🧠 Conceptual
intermediateWhy Are Intervals Important in Algorithms?
Which of the following best explains why intervals are a common problem pattern in algorithms?
Attempts:
2 left
💡 Hint
Think about what makes intervals tricky to handle in problems.
✗ Incorrect
Intervals often overlap, so algorithms must merge or split them to handle conflicts or coverage correctly. This pattern appears in many real-world problems like scheduling, resource allocation, and range queries.
🔧 Debug
advancedFind the Bug in Interval Overlap Check
What error does the following Rust code produce when checking if two intervals overlap?
```rust
fn is_overlapping(a: (i32, i32), b: (i32, i32)) -> bool {
a.1 < b.0 && b.1 > a.0
}
fn main() {
let interval1 = (1, 5);
let interval2 = (4, 8);
println!("{}", is_overlapping(interval1, interval2));
}
```
DSA Rust
fn is_overlapping(a: (i32, i32), b: (i32, i32)) -> bool { a.1 < b.0 && b.1 > a.0 } fn main() { let interval1 = (1, 5); let interval2 = (4, 8); println!("{}", is_overlapping(interval1, interval2)); }
Attempts:
2 left
💡 Hint
Check the logic condition carefully for overlap.
✗ Incorrect
The condition uses && but the correct overlap condition should use <= and >= with || or a different logic. This code returns false even when intervals overlap.
🚀 Application
advancedInterval Scheduling Maximum Non-overlapping Intervals
Given a list of intervals, which Rust code snippet correctly finds the maximum number of non-overlapping intervals?
DSA Rust
let intervals = vec![(1, 3), (2, 4), (3, 5), (7, 8)];
Attempts:
2 left
💡 Hint
Sort intervals by finish time to maximize non-overlapping intervals.
✗ Incorrect
The greedy algorithm sorts intervals by their end time and selects intervals that start after or at the end of the last selected interval. This maximizes the count of non-overlapping intervals.
🧠 Conceptual
expertWhy Are Interval Trees Useful?
Which statement best describes the main advantage of using an interval tree data structure?
Attempts:
2 left
💡 Hint
Think about what problem interval trees solve beyond simple sorting.
✗ Incorrect
Interval trees enable fast searching for all intervals that overlap with a query interval or point, which is useful in many applications like computational geometry and scheduling.
