Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort activities by their finish time.
DSA C++
sort(activities.begin(), activities.end(), [](auto a, auto b) { return a.finish [1] b.finish; });Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' will sort in descending order, which is incorrect.
Using '==' or '!=' does not define a strict ordering.
✗ Incorrect
We sort activities by their finish time in ascending order to select the earliest finishing activity first.
2fill in blank
mediumComplete the code to select the first activity after sorting.
DSA C++
selected.push_back(activities[[1]]);Attempts:
3 left
💡 Hint
Common Mistakes
Using index 1 skips the first activity.
Using activities.size() or activities.size() - 1 is out of range or selects the last activity.
✗ Incorrect
The first activity after sorting by finish time is at index 0 and 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] last_finish) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '<=' allows overlapping activities.
Using '==' is too strict and misses valid activities.
✗ Incorrect
We select the next activity only if its start time is greater than or equal to the finish time of the last selected activity.
4fill in blank
hardFill both blanks to update the last finish time and add the selected activity.
DSA C++
last_finish = activities[[1]].finish; selected.push_back(activities[[2]]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using i - 1 or 0 selects the wrong activity.
Using activities.size() - 1 selects the last activity, not the current one.
✗ Incorrect
We update last_finish to the finish time of the current activity at index i and add the same activity to the selected list.
5fill in blank
hardFill all three blanks to complete the activity selection loop.
DSA C++
for (int [1] = 1; [1] < activities.size(); [2]++) { if (activities[[1]].start [3] last_finish) { last_finish = activities[[1]].finish; selected.push_back(activities[[1]]); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using j as loop variable when i is used elsewhere.
Using '<=' in condition allows overlapping activities.
Using wrong increment variable causes infinite loop or errors.
✗ Incorrect
The loop variable is i starting from 1, incremented by i++, and the condition checks if the start time is greater than or equal to last_finish.
