0
0
DSA Typescriptprogramming~10 mins

Activity Selection Problem in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aduration
Bfinish
Cstart
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by start time instead of finish time.
Using a wrong property name.
2fill in blank
medium

Complete 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'
Aactivities.length - 1
B1
Cactivities.length
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Starting selection from index 1 instead of 0.
Using the last index instead of the first.
3fill in blank
hard

Fix 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'
Aactivities[i-1].finish
Bselected[selected.length]
Cselected[selected.length - 1].finish
Dactivities[0].start
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with the wrong activity's finish time.
Using an out-of-range index.
4fill in blank
hard

Fill 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'
A1
B0
Cselected[selected.length - 1].finish
Dactivities[0].start
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop from 0 causing duplicate selection.
Comparing with wrong finish time.
5fill in blank
hard

Fill 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'
Afinish
Bstart
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by start time instead of finish time.
Starting loop from 0 causing duplicate selection.