Challenge - 5 Problems
Activity Selection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Greedy Activity Selection
What is the output of the following Rust code that selects the maximum number of non-overlapping activities?
DSA Rust
fn activity_selection(start: &[i32], finish: &[i32]) -> Vec<usize> {
let n = start.len();
let mut selected = vec![0];
let mut last_finish = finish[0];
for i in 1..n {
if start[i] >= last_finish {
selected.push(i);
last_finish = finish[i];
}
}
selected
}
fn main() {
let start = [1, 3, 0, 5, 8, 5];
let finish = [2, 4, 6, 7, 9, 9];
let result = activity_selection(&start, &finish);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Remember the activities are selected based on earliest finish time and non-overlapping start times.
✗ Incorrect
The code selects activities starting with the first one (index 0). It then picks the next activity whose start time is at least the finish time of the last selected activity. The selected indices are [0, 1, 3, 4].
🧠 Conceptual
intermediateKey Idea Behind Activity Selection Problem
Which of the following best describes the main idea behind the greedy solution to the Activity Selection Problem?
Attempts:
2 left
💡 Hint
Think about how to leave room for as many activities as possible.
✗ Incorrect
The greedy approach picks the activity that finishes earliest to maximize the remaining time for other activities.
🔧 Debug
advancedIdentify the Bug in Activity Selection Code
What error will the following Rust code produce when run?
DSA Rust
fn activity_selection(start: &[i32], finish: &[i32]) -> Vec<usize> {
let n = start.len();
let mut selected = Vec::new();
let mut last_finish = finish[0];
for i in 1..n {
if start[i] > last_finish {
selected.push(i);
last_finish = finish[i];
}
}
selected
}
fn main() {
let start = [1, 3, 0, 5, 8, 5];
let finish = [2, 4, 6, 7, 9, 9];
let result = activity_selection(&start, &finish);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Check if the first activity is added to the selected list.
✗ Incorrect
The code never adds the first activity (index 0) to the selected list, so the output misses it.
❓ Predict Output
advancedOutput of Activity Selection with Unsorted Input
What is the output of this Rust code that attempts activity selection without sorting activities by finish time?
DSA Rust
fn activity_selection_unsorted(start: &mut [i32], finish: &mut [i32]) -> Vec<usize> {
let n = start.len();
let mut selected = vec![0];
let mut last_finish = finish[0];
for i in 1..n {
if start[i] >= last_finish {
selected.push(i);
last_finish = finish[i];
}
}
selected
}
fn main() {
let mut start = [5, 1, 3, 0, 8, 5];
let mut finish = [7, 2, 4, 6, 9, 9];
let result = activity_selection_unsorted(&mut start, &mut finish);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
The input is not sorted by finish time, so the greedy approach may fail.
✗ Incorrect
Without sorting by finish time, the code selects activities 0 and 4 only, missing others that could fit.
🧠 Conceptual
expertWhy Sorting by Finish Time is Crucial in Activity Selection
Why must activities be sorted by their finish times before applying the greedy activity selection algorithm?
Attempts:
2 left
💡 Hint
Think about how the earliest finishing activity affects the remaining time.
✗ Incorrect
Sorting by finish time ensures the greedy choice picks the earliest finishing activity, maximizing room for others.
