Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort activities by their finish time.
DSA Typescript
activities.sort((a, b) => a.[1] - b.finish); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by start time instead of finish time.
Using a wrong property name.
✗ Incorrect
Sorting activities by their finish time helps us select the maximum number of non-overlapping activities.
2fill in blank
mediumComplete the code to select the first activity after sorting.
DSA Typescript
selected.push(activities[[1]]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting selection from index 1 instead of 0.
Using the last index instead of the first.
✗ 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 Typescript
if (activities[i].start >= [1]) {
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with the wrong activity's finish time.
Using an out-of-range index.
✗ 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 Typescript
for (let i = [1]; i < activities.length; i++) { if (activities[i].start >= [2]) { selected.push(activities[i]); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop from 0 causing duplicate selection.
Comparing with wrong finish time.
✗ Incorrect
Start loop from 1 because first activity is already selected. Compare start time with last selected activity's finish time.
5fill in blank
hardFill all three blanks to create a function that returns the maximum set of compatible activities.
DSA Typescript
function selectActivities(activities: {start: number, finish: number}[]): {start: number, finish: number}[] {
activities.sort((a, b) => a.[1] - b.[2]);
const selected = [activities[0]];
for (let i = [3]; i < activities.length; i++) {
if (activities[i].start >= selected[selected.length - 1].finish) {
selected.push(activities[i]);
}
}
return selected;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by start time instead of finish time.
Starting loop from 0 causing duplicate selection.
✗ Incorrect
Sort by finish time (both a and b). Start loop from 1 since first activity is pre-selected.