Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort activities by their finish time.
DSA C#
activities.Sort((a, b) => a.[1] - b.[1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by start time instead of finish time.
Using duration which is not directly helpful here.
✗ Incorrect
Sorting activities by their finish time helps select the maximum number of non-overlapping activities.
2fill in blank
mediumComplete the code to select the first activity after sorting.
DSA C#
selectedActivities.Add(activities[[1]]);Attempts:
3 left
💡 Hint
Common Mistakes
Starting selection from index 1 instead of 0.
Using activities.Count which is out of range.
✗ Incorrect
The first activity in the sorted list is always selected first.
3fill in blank
hardFix the error in the condition to check if the next activity can be selected.
DSA C#
if (activities[i].start >= [1].finish)
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with activities[i-1] which may not be selected.
Using selectedActivities[0] which is the first selected, not the last.
✗ Incorrect
We compare the start time of the current activity with the finish time of the last selected activity.
4fill in blank
hardFill both blanks to complete the loop that selects compatible activities.
DSA C#
for (int i = [1]; i < activities.Count; i++) { if (activities[i].start >= selectedActivities.[2]().finish) { selectedActivities.Add(activities[i]); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop at 0 causing duplicate selection.
Using First() instead of Last() for comparison.
✗ Incorrect
Start loop from 1 because first activity is already selected; check start time against last selected activity's finish time.
5fill in blank
hardFill all three blanks to create a dictionary of selected activities with their start times as keys.
DSA C#
var result = new Dictionary<int, Activity>() {
{ [1], [2] }
};
foreach (var act in selectedActivities) {
result[act.[3]] = act;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using finish time as dictionary key which may cause conflicts.
Not initializing dictionary properly before adding activities.
✗ Incorrect
Initialize dictionary with first selected activity's start time as key and activity as value; then add others using start time as key.
