Challenge - 5 Problems
Activity Selection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Activity Selection Greedy Algorithm
What is the output of the following C# code that selects the maximum number of non-overlapping activities?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { int[] start = {1, 3, 0, 5, 8, 5}; int[] finish = {2, 4, 6, 7, 9, 9}; List<int> selected = ActivitySelection(start, finish); Console.WriteLine(string.Join(" -> ", selected) + " -> null"); } static List<int> ActivitySelection(int[] start, int[] finish) { List<int> result = new List<int>(); int n = start.Length; int i = 0; result.Add(i); for (int j = 1; j < n; j++) { if (start[j] >= finish[i]) { result.Add(j); i = j; } } return result; } }
Attempts:
2 left
💡 Hint
Trace the selection by comparing start times with the last selected finish time.
✗ Incorrect
The algorithm selects activities by earliest finish time. It starts with activity 0, then picks activity 1 (start 3 >= finish 2), then activity 3 (start 5 >= finish 4), then activity 4 (start 8 >= finish 7). So the selected activities are 0, 1, 3, 4.
🧠 Conceptual
intermediateUnderstanding Activity Selection Problem
Which of the following best describes the main goal of the Activity Selection Problem?
Attempts:
2 left
💡 Hint
Think about how many activities can be done without conflicts.
✗ Incorrect
The Activity Selection Problem aims to choose the largest set of activities that do not overlap, maximizing the number of activities one can attend.
🔧 Debug
advancedIdentify the Error in Activity Selection Code
What error will the following code produce when run?
DSA C#
int[] start = {1, 3, 0, 5, 8, 5}; int[] finish = {2, 4, 6, 7}; List<int> selected = ActivitySelection(start, finish); List<int> ActivitySelection(int[] start, int[] finish) { List<int> result = new List<int>(); int n = start.Length; int i = 0; result.Add(i); for (int j = 1; j < n; j++) { if (start[j] >= finish[i]) { result.Add(j); i = j; } } return result; }
Attempts:
2 left
💡 Hint
Check if both arrays have the same length.
✗ Incorrect
The finish array has fewer elements than the start array. Accessing finish[i] when i reaches 4 causes IndexOutOfRangeException.
🚀 Application
advancedMaximum Activities Count
Given start times [2, 1, 3, 0, 5, 8] and finish times [3, 2, 4, 6, 7, 9], what is the maximum number of non-overlapping activities that can be selected?
Attempts:
2 left
💡 Hint
Sort activities by finish time and select greedily.
✗ Incorrect
Sorted by finish time: activities with finish times 2,3,4,6,7,9. Selecting activities 1 (1-2), 0 (2-3), 2 (3-4), 4 (5-7), 5 (8-9) gives 5 activities without overlap.
❓ Predict Output
expertOutput of Activity Selection with Unsorted Input
What is the output of this C# code that attempts to select activities without sorting by finish time?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { int[] start = {4, 1, 3, 0, 5, 8}; int[] finish = {5, 2, 4, 6, 7, 9}; List<int> selected = ActivitySelection(start, finish); Console.WriteLine(string.Join(" -> ", selected) + " -> null"); } static List<int> ActivitySelection(int[] start, int[] finish) { List<int> result = new List<int>(); int n = start.Length; int i = 0; result.Add(i); for (int j = 1; j < n; j++) { if (start[j] >= finish[i]) { result.Add(j); i = j; } } return result; } }
Attempts:
2 left
💡 Hint
Without sorting by finish time, the greedy approach may select fewer activities.
✗ Incorrect
The code selects activity 0 (start 4, finish 5), then activity 4 (start 5 >= 5), then activity 5 (start 8 >= 7). So selected are 0, 4, 5.
