Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort activities by their finish time.
DSA Javascript
activities.sort((a, b) => a.[1] - b.finish);Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by start time instead of finish time.
Using a property that does not exist.
✗ Incorrect
We sort activities by their finish time to select the earliest finishing activity first.
2fill in blank
mediumComplete the code to select the first activity after the last selected one.
DSA Javascript
if (activities[i].start >= activities[[1]].finish) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed index like 0 instead of the last selected index.
Using i - 1 which may not be the last selected activity.
✗ Incorrect
We compare the start time of the current activity with the finish time of the last selected activity.
3fill in blank
hardFix the error in updating the last selected activity index.
DSA Javascript
lastSelected = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Assigning 0 which resets the last selected index incorrectly.
Assigning lastSelected to itself without change.
✗ Incorrect
We update lastSelected to the current activity index i after selecting it.
4fill in blank
hardFill both blanks to complete the loop that selects activities.
DSA Javascript
for (let i = [1]; i < activities.length; i++) { if (activities[i].start >= activities[[2]].finish) { selected.push(activities[i]); lastSelected = i; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop at 0 causing duplicate selection.
Comparing with wrong index.
✗ Incorrect
Start loop from 1 because first activity is selected. Compare with lastSelected index.
5fill in blank
hardFill all three blanks to create the function that returns selected activities.
DSA Javascript
function selectActivities(activities) {
activities.sort((a, b) => a.[1] - b.[2]);
let selected = [activities[0]];
let lastSelected = 0;
for (let i = [3]; i < activities.length; i++) {
if (activities[i].start >= activities[lastSelected].finish) {
selected.push(activities[i]);
lastSelected = i;
}
}
return selected;
}Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by start time instead of finish time.
Starting loop at 0 causing duplicate selection.
✗ Incorrect
Sort by finish time (both blanks), loop starts at 1 after selecting first activity.
