0
0
DSA Cprogramming~20 mins

Activity Selection Problem in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Activity Selection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
ASelected activities: 0 1 3 4
BSelected activities: 0 2 3 5
CSelected activities: 1 3 4
DSelected activities: 0 1 4
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.
🧠 Conceptual
intermediate
1:30remaining
Key Idea Behind Activity Selection Problem
What is the main idea behind the greedy solution to the Activity Selection Problem?
AAlways select the activity with the earliest start time.
BAlways select the activity with the shortest duration.
CAlways select the activity that starts latest among the remaining activities.
DAlways select the activity that finishes earliest among the remaining activities.
Attempts:
2 left
💡 Hint
Think about which choice leaves the most room for other activities.
🔧 Debug
advanced
2: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;
}
ARuns correctly and prints selected activities.
BSegmentation fault due to out-of-bounds array access.
CCompilation error due to missing semicolon.
DLogical error: prints wrong activities but no crash.
Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
🚀 Application
advanced
1: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?
A4
B3
C5
D6
Attempts:
2 left
💡 Hint
Sort activities by finish time and pick greedily.
🧠 Conceptual
expert
1: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?
AIt ensures the earliest starting activities are chosen first.
BIt simplifies the code but does not affect the correctness of the solution.
CIt guarantees the maximum number of activities can be selected by always choosing the earliest finishing activity.
DIt helps to select activities with the longest duration first.
Attempts:
2 left
💡 Hint
Think about how finish times affect the possibility of scheduling more activities.