Challenge - 5 Problems
Activity Selection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Greedy Activity Selection
What is the output of the following C code that selects the maximum number of non-overlapping activities?
DSA C
#include <stdio.h> void printMaxActivities(int start[], int finish[], int n) { int i, j; printf("Selected activities: "); i = 0; printf("%d ", i); for (j = 1; j < n; j++) { if (start[j] >= finish[i]) { printf("%d ", j); i = j; } } printf("\n"); } int main() { int start[] = {1, 3, 0, 5, 8, 5}; int finish[] = {2, 4, 6, 7, 9, 9}; int n = sizeof(start)/sizeof(start[0]); printMaxActivities(start, finish, n); return 0; }
Attempts:
2 left
💡 Hint
Remember the greedy approach selects the first activity and then picks the next activity whose start time is greater or equal to the finish time of the last selected activity.
✗ Incorrect
The code selects activities by iterating through the list and choosing the next activity that starts after or when the last selected activity finishes. The selected indices are 0, 1, 3, and 4.
🧠 Conceptual
intermediate1:30remaining
Key Idea Behind Activity Selection Problem
What is the main 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 allows more activities to fit after it, maximizing the total number of activities selected.
🔧 Debug
advanced2:00remaining
Identify the Bug in Activity Selection Code
What error will the following code produce when run?
DSA C
#include <stdio.h> void printMaxActivities(int start[], int finish[], int n) { int i = 0, j; printf("Selected activities: %d ", i); for (j = 1; j < n; j++) { if (start[j] >= finish[i]) { printf("%d ", j); i = j; } } printf("\n"); } int main() { int start[] = {1, 3, 0, 5, 8, 5}; int finish[] = {2, 4, 6, 7, 9, 9}; int n = sizeof(start)/sizeof(start[0]); printMaxActivities(start, finish, n); return 0; }
Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
✗ Incorrect
The loop runs with j <= n, which causes j to reach n, an invalid index for arrays of size n (0-based indexing). This leads to out-of-bounds access and segmentation fault.
🚀 Application
advanced1:30remaining
Maximum Number of Activities Selected
Given the start times [2, 1, 3, 0, 5, 8] and finish times [3, 2, 4, 6, 7, 9], how many activities can be selected without overlapping?
Attempts:
2 left
💡 Hint
Sort activities by finish time and pick greedily.
✗ Incorrect
Sorted by finish time: activities with finish times 2,3,4,6,7,9. Selecting activities 1,0,2,4 gives 4 activities without overlap.
🧠 Conceptual
expert1:30remaining
Why Sorting by Finish Time is Crucial?
Why is sorting activities by their finish times essential in the greedy solution to the Activity Selection Problem?
Attempts:
2 left
💡 Hint
Think about how finish times affect the possibility of scheduling more activities.
✗ Incorrect
Sorting by finish time allows the algorithm to pick the activity that leaves the most room for others, ensuring the maximum number of activities are selected.