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
Why do we sort activities by their finishing times in the Activity Selection Problem?
Sorting by finishing times helps pick the activity that leaves the most room for others, maximizing the total number of activities selected.
Click to reveal answer
beginner
In the Activity Selection Problem, what condition must the next chosen activity satisfy?
Its start time must be greater than or equal to the finish time of the last selected activity.
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 finish time; the selection process itself is O(n).
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 selecting an activity, what is the next step in the greedy algorithm for the Activity Selection Problem?
ASelect the activity with the earliest start time
BSelect the activity with the latest finishing time
CSelect the activity with the longest duration
DSelect the next activity that starts after the current one finishes
✗ Incorrect
The algorithm picks the next activity whose start time is at or after the finish time of the last selected activity.
What does the Activity Selection Problem help to optimize?
AMinimum number of activities
BMaximum number of non-overlapping activities
CMaximum total duration of activities
DMinimum total duration of activities
✗ Incorrect
The goal is to select the largest set of activities that do not overlap in time.
Which of these is NOT true about the greedy solution to the Activity Selection Problem?
AIt selects activities based on earliest start time
BIt requires sorting activities by finish time
CIt always produces an optimal solution
DIt runs in O(n log n) time due to sorting
✗ Incorrect
The greedy solution selects activities based on earliest finish time, not start time.
If two activities have the same finishing time, which one should the greedy algorithm pick first?
AThe one with the earlier start time
BThe one with the later start time
CEither one, since they finish at the same time
DThe one with the longer duration
✗ Incorrect
Since they finish at the same time, either can be picked; the greedy choice property holds regardless, ensuring optimality.
Explain the steps of the greedy algorithm used to solve the Activity Selection Problem.
Think about how sorting helps and how you pick activities one by one.
You got /5 concepts.
Why does sorting activities by their finishing times guarantee an optimal solution in the Activity Selection Problem?
Consider how choosing 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. Select the maximum number of activities that don't overlap in time
B. Find the activity with the longest duration
C. Sort activities by their start times
D. Calculate the total time of all activities
Solution
Step 1: Understand the problem goal
The Activity Selection Problem aims to choose the largest set of activities that do not overlap in their time intervals.
Step 2: Clarify what is NOT the goal
Finding the longest activity or sorting alone does not solve the problem; the focus is on maximizing non-overlapping activities.
Final Answer:
Select the maximum number of activities that don't overlap in time -> Option A
Quick Check:
Maximize non-overlapping activities = Select max activities [OK]
Hint: Focus on non-overlapping activities to maximize count [OK]
Common Mistakes:
Thinking it's about longest activity duration
Confusing sorting with selection
Trying to maximize total time instead of count
2. Which of the following is the correct way to sort activities by their finish time in JavaScript?
easy
A. activities.sort((a, b) => a.finish - b.finish);
B. activities.sort((a, b) => a.start - b.start);
C. activities.sort((a, b) => b.finish - a.finish);
D. activities.sort((a, b) => a.duration - b.duration);
Solution
Step 1: Identify sorting key for Activity Selection
We must sort activities by their finish time in ascending order to apply the greedy approach.
Step 2: Choose correct JavaScript sort syntax
The correct syntax is activities.sort((a, b) => a.finish - b.finish); which sorts by finish time ascending.
Final Answer:
activities.sort((a, b) => a.finish - b.finish); -> Option A
Quick Check:
Sort by finish time ascending = activities.sort((a, b) => a.finish - b.finish); [OK]
Hint: Sort activities by finish time ascending for greedy selection [OK]
Common Mistakes:
Sorting by start time instead of finish time
Sorting finish time descending
Sorting by duration which is irrelevant here
3. What will be the output of the following code snippet?
const activities = [
{start: 1, finish: 4},
{start: 3, finish: 5},
{start: 0, finish: 6},
{start: 5, finish: 7},
{start: 8, finish: 9},
{start: 5, finish: 9}
];
activities.sort((a, b) => a.finish - b.finish);
let count = 1;
let lastFinish = activities[0].finish;
for (let i = 1; i < activities.length; i++) {
if (activities[i].start >= lastFinish) {
count++;
lastFinish = activities[i].finish;
}
}
console.log(count);
medium
A. 6
B. 3
C. 5
D. 4
Solution
Step 1: Sort activities by finish time
Sorted activities by finish time ascending: [{start:1, finish:4}, {start:3, finish:5}, {start:0, finish:6}, {start:5, finish:7}, {start:8, finish:9}, {start:5, finish:9}].
Step 2: Select activities greedily
Start with first activity (finish=4). Next, select activities starting >=4: (start=5, finish=7), count=2; then (start=8, finish=9), count=3; then (start=9, finish=9) does not exist but next is (start=5, finish=9) which starts before 9, so skip. Total count is 4.
Final Answer:
4 -> Option D
Quick Check:
Greedy selection count = 4 [OK]
Hint: Sort by finish, pick next starting after last finish [OK]
Common Mistakes:
Not sorting activities before selection
Counting overlapping activities
Updating lastFinish incorrectly
4. Identify the bug in the following code for Activity Selection Problem:
function selectActivities(activities) {
activities.sort((a, b) => a.start - b.start);
let count = 1;
let lastFinish = activities[0].finish;
for (let i = 1; i < activities.length; i++) {
if (activities[i].start > lastFinish) {
count++;
lastFinish = activities[i].finish;
}
}
return count;
}
medium
A. Initialize count to 0 instead of 1
B. Comparison should be activities[i].start >= lastFinish
C. Sorting should be by finish time, not start time
D. No bug, code is correct
Solution
Step 1: Check sorting criteria
The greedy approach requires sorting activities by their finish time, but the code sorts by start time, which is incorrect.
Step 2: Understand impact of wrong sorting
Sorting by start time can cause wrong selection order, leading to suboptimal count of activities.
Final Answer:
Sorting should be by finish time, not start time -> Option C
Quick Check:
Sort by finish time for correct greedy selection [OK]
Hint: Always sort by finish time, not start time [OK]
Common Mistakes:
Sorting by start time instead of finish time
Using > instead of >= in condition (less critical)
Incorrect initialization of count
5. Given the activities with start and finish times: