0
0
DSA Typescriptprogramming~20 mins

Activity Selection Problem in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Activity Selection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Activity Selection Greedy Algorithm
What is the output of the following TypeScript code that selects the maximum number of non-overlapping activities?
DSA Typescript
interface Activity {
  start: number;
  finish: number;
}

function activitySelection(activities: Activity[]): Activity[] {
  activities.sort((a, b) => a.finish - b.finish);
  const selected: Activity[] = [];
  let lastFinish = 0;
  for (const activity of activities) {
    if (activity.start >= lastFinish) {
      selected.push(activity);
      lastFinish = activity.finish;
    }
  }
  return selected;
}

const activities = [
  { start: 1, finish: 4 },
  { start: 3, finish: 5 },
  { start: 0, finish: 6 },
  { start: 5, finish: 7 },
  { start: 8, finish: 9 },
  { start: 5, finish: 9 }
];

const result = activitySelection(activities);
console.log(result.map(a => `[${a.start},${a.finish}]`).join(' -> '));
A[0,6] -> [5,7] -> [8,9]
B[1,4] -> [5,7] -> [8,9]
C[1,4] -> [3,5] -> [5,7] -> [8,9]
D[3,5] -> [5,7] -> [8,9]
Attempts:
2 left
💡 Hint
Sort activities by their finish time and pick the earliest finishing activity that starts after the last selected one.
🧠 Conceptual
intermediate
1:00remaining
Maximum Number of Activities Selected
Given the activities with start and finish times: (1,3), (2,5), (4,7), (6,9), (8,10), how many activities can be selected without overlap using the greedy approach?
A3
B4
C2
D5
Attempts:
2 left
💡 Hint
Sort by finish time and pick activities that start after the last selected finish time.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Activity Selection Code
What error does the following TypeScript code produce when run?
DSA Typescript
interface Activity {
  start: number;
  finish: number;
}

function activitySelection(activities: Activity[]): Activity[] {
  activities.sort((a, b) => a.start - b.start);
  const selected: Activity[] = [];
  let lastFinish = 0;
  for (const activity of activities) {
    if (activity.start >= lastFinish) {
      selected.push(activity);
      lastFinish = activity.finish;
    }
  }
  return selected;
}

const activities = [
  { start: 1, finish: 4 },
  { start: 3, finish: 5 },
  { start: 0, finish: 6 },
  { start: 5, finish: 7 },
  { start: 8, finish: 9 },
  { start: 5, finish: 9 }
];

const result = activitySelection(activities);
console.log(result.map(a => `[${a.start},${a.finish}]`).join(' -> '));
ANo error, outputs '[1,4] -> [5,7] -> [8,9]'
BNo error, outputs '[1,4] -> [3,5] -> [5,7] -> [8,9]'
CNo error, outputs '[0,6] -> [8,9]'
DNo error, outputs '[0,6] -> [5,7] -> [8,9]'
Attempts:
2 left
💡 Hint
Check how activities are sorted before selection.
🚀 Application
advanced
1:30remaining
Find Selected Activities for Given Intervals
Given these activities: (2,4), (3,5), (1,3), (5,7), (6,8), (8,10), which activities will be selected by the greedy algorithm that sorts by finish time?
A(1,3), (3,5), (5,7), (8,10)
B(1,3), (2,4), (5,7), (8,10)
C(2,4), (5,7), (6,8), (8,10)
D(1,3), (5,7), (8,10)
Attempts:
2 left
💡 Hint
Sort activities by finish time and pick earliest finishing compatible activities.
🧠 Conceptual
expert
1:30remaining
Why Sorting by Finish Time is Optimal in Activity Selection
Why does sorting activities by their finish time guarantee the maximum number of non-overlapping activities selected?
ABecause sorting by finish time ensures activities are selected in the order they were added.
BBecause sorting by start time always selects the longest activities first.
CBecause sorting by finish time minimizes the total duration of selected activities.
DBecause selecting the earliest finishing activity leaves maximum room for remaining activities.
Attempts:
2 left
💡 Hint
Think about how choosing the earliest finishing activity affects the remaining time.