What is the main goal of the Activity Selection Problem?
To select the maximum number of activities that don't overlap in time from a given set of activities.
Click to reveal answer
beginner
Which algorithmic approach is commonly used to solve the Activity Selection Problem?
A greedy algorithm that selects activities based on earliest finishing time first.
Click to reveal answer
intermediate
In the Activity Selection Problem, why do we sort activities by their finishing times?
Sorting by finishing times ensures that we always pick the activity that leaves the most room for others, maximizing the total number of activities selected.
Click to reveal answer
intermediate
What is the time complexity of the greedy solution to the Activity Selection Problem after sorting?
O(n log n) due to sorting the activities by their finishing times; the selection process itself is O(n).
Click to reveal answer
advanced
Explain the greedy choice property in the context of the Activity Selection Problem.
Choosing the activity that finishes earliest is a greedy choice because it leaves the maximum remaining time for other activities, leading to an optimal solution.
Click to reveal answer
What is the first step in solving the Activity Selection Problem using a greedy approach?
ASort activities by their finishing times
BSort activities by their starting times
CSelect the longest activity first
DSelect activities randomly
✗ Incorrect
Sorting activities by finishing times allows the greedy algorithm to pick the earliest finishing activity first.
After sorting activities by finishing time, how do you select the next activity?
AChoose the activity with the earliest start time
BChoose the next activity that starts before the last selected activity finishes
CChoose the activity with the longest duration
DChoose the next activity that starts after the last selected activity finishes
✗ Incorrect
To avoid overlap, the next activity must start after the last selected activity finishes.
What does the Activity Selection Problem demonstrate about greedy algorithms?
AThey are slower than dynamic programming
BThey always find the optimal solution for this problem
CThey sometimes fail to find the optimal solution
DThey require backtracking
✗ Incorrect
The greedy algorithm for Activity Selection always finds the optimal solution because of the problem's greedy choice property.
Which data structure is most useful to store activities for the Activity Selection Problem?
AArray or list of tuples (start, finish)
BStack
CQueue
DHash map
✗ Incorrect
An array or list of (start, finish) pairs is used to represent activities and sort them.
What is the output of the Activity Selection Problem?
ATotal duration of all activities
BMinimum number of activities
CMaximum set of non-overlapping activities
DAll activities sorted by start time
✗ Incorrect
The problem outputs the largest possible set of activities that do not overlap.
Describe the steps to solve the Activity Selection Problem using a greedy algorithm.
Think about sorting and then picking activities that don't overlap.
You got /5 concepts.
Explain why the greedy choice of selecting the earliest finishing activity leads to an optimal solution in the Activity Selection Problem.
Consider how picking the earliest finishing activity affects the rest.
You got /4 concepts.
Practice
(1/5)
1. What is the main goal of the Activity Selection Problem?
easy
A. Calculate the total duration of all activities
B. Select the maximum number of activities that don't overlap in time
C. Sort activities by their start time
D. Find the activity with the longest duration
Solution
Step 1: Understand the problem objective
The Activity Selection Problem aims to choose the largest set of activities that do not overlap in their time intervals.
Step 2: Identify the correct goal
Among the options, only selecting the maximum number of non-overlapping activities matches the problem's goal.
Final Answer:
Select the maximum number of activities that don't overlap in time -> Option B
Quick Check:
Goal = Max non-overlapping activities [OK]
Hint: Focus on maximizing non-overlapping activities [OK]
Common Mistakes:
Confusing longest duration with maximum count
Sorting by start time only
Summing durations instead of counting activities
2. Which Rust code snippet correctly sorts a vector of activities by their finish time? Assume each activity is a tuple (start: i32, finish: i32).
easy
A. activities.sort_by_key(|a| a.1);
B. activities.sort_by_key(|a| a.0);
C. activities.sort_unstable_by(|a, b| b.1.cmp(&a.1));
D. activities.sort_by(|a, b| a.0.cmp(&b.0));
Solution
Step 1: Identify sorting key for finish time
Each activity is a tuple (start, finish). The finish time is at index 1.
Step 2: Choose correct sorting method
Sorting by finish time ascending uses sort_by_key(|a| a.1). activities.sort_by_key(|a| a.1); matches this.
Final Answer:
activities.sort_by_key(|a| a.1); -> Option A
Quick Check:
Sort by finish time = a.1 [OK]
Hint: Sort activities by finish time using .sort_by_key(|a| a.1) [OK]
Common Mistakes:
Sorting by start time instead of finish time
Sorting in descending order incorrectly
Using unstable sort unnecessarily
3. What is the output of the following Rust code that implements the activity selection logic?
let mut activities = vec![(1, 4), (3, 5), (0, 6), (5, 7), (8, 9), (5, 9)];
activities.sort_by_key(|a| a.1);
let mut selected = vec![activities[0]];
let mut last_finish = activities[0].1;
for &activity in &activities[1..] {
if activity.0 >= last_finish {
selected.push(activity);
last_finish = activity.1;
}
}
println!("{:?}", selected);
Start with (1,4). Next, (3,5) starts before 4, skip. (0,6) starts before 4, skip. (5,7) starts at 5 ≥ 4, select. (8,9) starts at 8 ≥ 7, select. (5,9) starts at 5 < 7, skip.
Final Answer:
[(1, 4), (5, 7), (8, 9)] -> Option C
Quick Check:
Greedy selection = [(1,4),(5,7),(8,9)] [OK]
Hint: Pick earliest finishing activities that start after last finish [OK]
Common Mistakes:
Selecting overlapping activities
Not sorting by finish time first
Including activities starting before last finish
4. Identify the bug in this Rust code snippet for the Activity Selection Problem:
let mut activities = vec![(1, 3), (2, 5), (4, 7)];
activities.sort_by_key(|a| a.0);
let mut selected = vec![activities[0]];
let mut last_finish = activities[0].1;
for &activity in &activities[1..] {
if activity.0 > last_finish {
selected.push(activity);
last_finish = activity.1;
}
}
println!("{:?}", selected);
medium
A. Using > instead of >= in the if condition
B. Not pushing the first activity into selected
C. Not initializing last_finish correctly
D. Sorting by start time instead of finish time
Solution
Step 1: Check sorting criteria
The code sorts activities by start time (a.0), but the greedy approach requires sorting by finish time.
Step 2: Understand impact of sorting
Sorting by start time breaks the greedy logic, causing incorrect selection of activities.
Final Answer:
Sorting by start time instead of finish time -> Option D
Quick Check:
Sort by finish time needed, not start time [OK]
Hint: Always sort activities by finish time, not start time [OK]
Common Mistakes:
Sorting by start time
Using strict > instead of >= (less critical)
Incorrect initialization of last_finish
5. Given a list of activities with start and finish times, how would you modify the standard Activity Selection Problem to select activities that maximize total duration instead of count? Which approach is correct?
hard
A. Use dynamic programming to consider all subsets and maximize total duration
B. Sort activities by start time and greedily pick those with longest duration
C. Sort by finish time and pick activities with shortest duration first
D. Select activities randomly and check total duration
Solution
Step 1: Understand difference in optimization goal
Maximizing total duration is different from maximizing count; greedy approach fails here.
Step 2: Choose correct algorithmic approach
Dynamic programming can consider all subsets to find the maximum total duration without overlaps.
Final Answer:
Use dynamic programming to consider all subsets and maximize total duration -> Option A
Quick Check:
Max total duration needs DP, not greedy [OK]
Hint: Max duration needs DP, not greedy selection [OK]
Common Mistakes:
Trying greedy for max duration
Sorting by start time only
Random selection without logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.