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 C++ code that selects the maximum number of activities that don't overlap?
DSA C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Activity { int start, finish; }; bool activityCompare(Activity s1, Activity s2) { return (s1.finish < s2.finish); } int main() { vector<Activity> activities = {{1, 4}, {3, 5}, {0, 6}, {5, 7}, {8, 9}, {5, 9}}; sort(activities.begin(), activities.end(), activityCompare); int count = 1; int last_finish = activities[0].finish; cout << "Selected activities (start, finish): (" << activities[0].start << ", " << activities[0].finish << ")"; for (int i = 1; i < activities.size(); i++) { if (activities[i].start >= last_finish) { cout << " -> (" << activities[i].start << ", " << activities[i].finish << ")"; last_finish = activities[i].finish; count++; } } cout << "\nTotal selected: " << count << endl; return 0; }
Attempts:
2 left
💡 Hint
Sort activities by their finish time and pick the earliest finishing activity first.
✗ Incorrect
The greedy approach sorts activities by finish time and selects the first activity. Then it selects the next activity whose start time is after or equal to the last selected activity's finish time. This ensures maximum number of non-overlapping activities.
🧠 Conceptual
intermediateKey Idea Behind Activity Selection Problem
Which of the following best describes the key idea behind the greedy solution to the Activity Selection Problem?
Attempts:
2 left
💡 Hint
Think about which choice leaves the most room for other activities.
✗ Incorrect
Selecting the activity that finishes earliest leaves the maximum remaining time for other activities, allowing the maximum number of activities to be selected.
🔧 Debug
advancedIdentify the Bug in Activity Selection Code
What error will the following code produce when run, and why?
DSA C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Activity { int start, finish; }; bool activityCompare(Activity s1, Activity s2) { return (s1.start < s2.start); } int main() { vector<Activity> activities = {{1, 4}, {3, 5}, {0, 6}, {5, 7}, {8, 9}, {5, 9}}; sort(activities.begin(), activities.end(), activityCompare); int count = 1; int last_finish = activities[0].finish; cout << "Selected activities (start, finish): (" << activities[0].start << ", " << activities[0].finish << ")"; for (int i = 1; i < activities.size(); i++) { if (activities[i].start >= last_finish) { cout << " -> (" << activities[i].start << ", " << activities[i].finish << ")"; last_finish = activities[i].finish; count++; } } cout << "\nTotal selected: " << count << endl; return 0; }
Attempts:
2 left
💡 Hint
Check the sorting criteria and how it affects the greedy choice.
✗ Incorrect
Sorting by start time does not guarantee the earliest finishing activity is chosen first, so the greedy approach fails to select the maximum number of activities.
🚀 Application
advancedMaximum Number of Activities Selected
Given the activities with start and finish times: (2, 3), (1, 4), (3, 5), (0, 7), (5, 8), (5, 9), (6, 10), (8, 11), (8, 12), (2, 13), (12, 14), how many activities can be selected using the greedy activity selection algorithm?
Attempts:
2 left
💡 Hint
Sort activities by finish time and select greedily.
✗ Incorrect
After sorting by finish time, the selected activities are (2,3), (3,5), (5,8), (8,11), (12,14), (8,12) is skipped because it overlaps with (8,11), so total 5 activities can be selected.
🧠 Conceptual
expertWhy Does Greedy Work for Activity Selection?
Why does the greedy algorithm that selects the earliest finishing activity at each step guarantee an optimal solution for the Activity Selection Problem?
Attempts:
2 left
💡 Hint
Think about the properties that allow greedy algorithms to be optimal.
✗ Incorrect
The problem exhibits optimal substructure and greedy choice property: choosing the earliest finishing activity leaves the largest possible remaining time for other activities, ensuring an optimal solution.
