Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Activity Selection Problem in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 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 Algorithm
What is the output of the following JavaScript code that selects the maximum number of non-overlapping activities?
DSA Javascript
function activitySelection(start, end) {
  const n = start.length;
  let selected = [];
  let i = 0;
  selected.push(i);
  for (let j = 1; j < n; j++) {
    if (start[j] >= end[i]) {
      selected.push(j);
      i = j;
    }
  }
  return selected;
}

const start = [1, 3, 0, 5, 8, 5];
const end = [2, 4, 6, 7, 9, 9];
console.log(activitySelection(start, end));
A[0, 1, 3, 4]
B[0, 1, 4]
C[1, 3, 4]
D[0, 2, 3, 5]
Attempts:
2 left
💡 Hint
Remember the greedy approach selects the first activity and then picks the next activity whose start time is greater or equal to the last selected activity's end time.
🧠 Conceptual
intermediate
1:30remaining
Understanding the Greedy Choice in Activity Selection
Why does the greedy algorithm for the Activity Selection Problem always pick the activity with the earliest finishing time first?
ABecause the earliest finishing activity always has the longest duration.
BBecause it selects activities randomly to avoid bias.
CBecause it minimizes the total number of activities selected.
DBecause choosing the earliest finishing activity leaves maximum room for the remaining activities.
Attempts:
2 left
💡 Hint
Think about how finishing early affects the possibility of selecting more activities later.
❓ Predict Output
advanced
2:00remaining
Output with Unsorted Activities
What will be the output of this code when the activities are not sorted by their finishing times?
DSA Javascript
function activitySelectionUnsorted(start, end) {
  const n = start.length;
  let activities = [];
  for (let i = 0; i < n; i++) {
    activities.push({start: start[i], end: end[i], index: i});
  }
  let selected = [];
  let i = 0;
  selected.push(activities[i].index);
  for (let j = 1; j < n; j++) {
    if (activities[j].start >= activities[i].end) {
      selected.push(activities[j].index);
      i = j;
    }
  }
  return selected;
}

const start = [1, 3, 0, 5, 8, 5];
const end = [2, 4, 6, 7, 9, 9];
console.log(activitySelectionUnsorted(start, end));
A[0, 3, 4]
B[0, 1, 3, 4]
C[0, 2, 5]
D[0, 1, 4]
Attempts:
2 left
💡 Hint
The activities are not sorted by finishing time, so the greedy approach may fail to select the maximum set.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Activity Selection Code
What error will this code produce when run, and why?
DSA Javascript
function activitySelectionBug(start, end) {
  const n = start.length;
  let selected = [];
  let i = 0;
  selected.push(i);
  for (let j = 1; j < n; j++) {
    if (start[j] > end[i]) {
      selected.push(j);
      i = j;
    }
  }
  return selected;
}

const start = [1, 3, 0, 5, 8, 5];
const end = [2, 4, 6, 7, 9, 9];
console.log(activitySelectionBug(start, end));
AOutputs [0, 1, 3, 4]
BOutputs [0, 3, 4]
COutputs [0, 1, 4]
DOutputs [0, 1, 3, 4, 5]
Attempts:
2 left
💡 Hint
Check the condition for selecting the next activity carefully.
🚀 Application
expert
3:00remaining
Maximum Number of Activities with Additional Constraints
Given activities with start and end times, and a mandatory rest time of 1 unit after each activity, which code modification correctly selects the maximum number of activities?
DSA Javascript
function activitySelectionWithRest(start, end) {
  const n = start.length;
  let selected = [];
  let i = 0;
  selected.push(i);
  for (let j = 1; j < n; j++) {
    if (start[j] >= end[i] + 1) {
      selected.push(j);
      i = j;
    }
  }
  return selected;
}

const start = [1, 3, 0, 5, 8, 5];
const end = [2, 4, 6, 7, 9, 9];
console.log(activitySelectionWithRest(start, end));
AKeep condition 'start[j] >= end[i]' and sort activities by start time.
BChange condition to 'start[j] >= end[i] + 1' and sort activities by end time before selection.
CChange condition to 'start[j] > end[i]' without sorting activities.
DChange condition to 'start[j] > end[i] + 1' and do not sort activities.
Attempts:
2 left
💡 Hint
Remember the greedy algorithm requires sorting by finishing time and the rest time affects the selection condition.

Practice

(1/5)
1. What is the main goal of the Activity Selection Problem?
easy
A. Select the maximum number of activities that don't overlap in time
B. Find the activity with the longest duration
C. Sort activities by their start times
D. Calculate the total time of all activities

Solution

  1. Step 1: Understand the problem goal

    The Activity Selection Problem aims to choose the largest set of activities that do not overlap in their time intervals.
  2. Step 2: Clarify what is NOT the goal

    Finding the longest activity or sorting alone does not solve the problem; the focus is on maximizing non-overlapping activities.
  3. Final Answer:

    Select the maximum number of activities that don't overlap in time -> Option A
  4. Quick Check:

    Maximize non-overlapping activities = Select max activities [OK]
Hint: Focus on non-overlapping activities to maximize count [OK]
Common Mistakes:
  • Thinking it's about longest activity duration
  • Confusing sorting with selection
  • Trying to maximize total time instead of count
2. Which of the following is the correct way to sort activities by their finish time in JavaScript?
easy
A. activities.sort((a, b) => a.finish - b.finish);
B. activities.sort((a, b) => a.start - b.start);
C. activities.sort((a, b) => b.finish - a.finish);
D. activities.sort((a, b) => a.duration - b.duration);

Solution

  1. Step 1: Identify sorting key for Activity Selection

    We must sort activities by their finish time in ascending order to apply the greedy approach.
  2. Step 2: Choose correct JavaScript sort syntax

    The correct syntax is activities.sort((a, b) => a.finish - b.finish); which sorts by finish time ascending.
  3. Final Answer:

    activities.sort((a, b) => a.finish - b.finish); -> Option A
  4. Quick Check:

    Sort by finish time ascending = activities.sort((a, b) => a.finish - b.finish); [OK]
Hint: Sort activities by finish time ascending for greedy selection [OK]
Common Mistakes:
  • Sorting by start time instead of finish time
  • Sorting finish time descending
  • Sorting by duration which is irrelevant here
3. What will be the output of the following code snippet?
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}
];

activities.sort((a, b) => a.finish - b.finish);

let count = 1;
let lastFinish = activities[0].finish;

for (let i = 1; i < activities.length; i++) {
  if (activities[i].start >= lastFinish) {
    count++;
    lastFinish = activities[i].finish;
  }
}
console.log(count);
medium
A. 6
B. 3
C. 5
D. 4

Solution

  1. Step 1: Sort activities by finish time

    Sorted activities by finish time ascending: [{start:1, finish:4}, {start:3, finish:5}, {start:0, finish:6}, {start:5, finish:7}, {start:8, finish:9}, {start:5, finish:9}].
  2. Step 2: Select activities greedily

    Start with first activity (finish=4). Next, select activities starting >=4: (start=5, finish=7), count=2; then (start=8, finish=9), count=3; then (start=9, finish=9) does not exist but next is (start=5, finish=9) which starts before 9, so skip. Total count is 4.
  3. Final Answer:

    4 -> Option D
  4. Quick Check:

    Greedy selection count = 4 [OK]
Hint: Sort by finish, pick next starting after last finish [OK]
Common Mistakes:
  • Not sorting activities before selection
  • Counting overlapping activities
  • Updating lastFinish incorrectly
4. Identify the bug in the following code for Activity Selection Problem:
function selectActivities(activities) {
  activities.sort((a, b) => a.start - b.start);
  let count = 1;
  let lastFinish = activities[0].finish;
  for (let i = 1; i < activities.length; i++) {
    if (activities[i].start > lastFinish) {
      count++;
      lastFinish = activities[i].finish;
    }
  }
  return count;
}
medium
A. Initialize count to 0 instead of 1
B. Comparison should be activities[i].start >= lastFinish
C. Sorting should be by finish time, not start time
D. No bug, code is correct

Solution

  1. Step 1: Check sorting criteria

    The greedy approach requires sorting activities by their finish time, but the code sorts by start time, which is incorrect.
  2. Step 2: Understand impact of wrong sorting

    Sorting by start time can cause wrong selection order, leading to suboptimal count of activities.
  3. Final Answer:

    Sorting should be by finish time, not start time -> Option C
  4. Quick Check:

    Sort by finish time for correct greedy selection [OK]
Hint: Always sort by finish time, not start time [OK]
Common Mistakes:
  • Sorting by start time instead of finish time
  • Using > instead of >= in condition (less critical)
  • Incorrect initialization of count
5. Given the activities with start and finish times:
const activities = [
  {start: 2, finish: 3},
  {start: 1, finish: 4},
  {start: 5, finish: 7},
  {start: 6, finish: 9},
  {start: 8, finish: 10},
  {start: 9, finish: 11}
];

What is the maximum number of non-overlapping activities that can be selected?
hard
A. 6
B. 4
C. 5
D. 3

Solution

  1. Step 1: Sort activities by finish time

    Sorted activities by finish time ascending: [{start:2, finish:3}, {start:1, finish:4}, {start:5, finish:7}, {start:6, finish:9}, {start:8, finish:10}, {start:9, finish:11}].
  2. Step 2: Select activities greedily

    Select (2,3), then (5,7), then (8,10), then (9,11) overlaps with (8,10) so skip; but (6,9) overlaps with (5,7) so skip. Total count is 4.
  3. Step 3: Check for more selections

    Others overlap and cannot be selected without violating non-overlap. Maximum count is 4.
  4. Final Answer:

    4 -> Option B
  5. Quick Check:

    Max non-overlapping activities = 4 [OK]
Hint: Sort by finish time, pick next starting after last finish [OK]
Common Mistakes:
  • Not sorting activities before selection
  • Selecting overlapping activities
  • Ignoring start time conditions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard