Bird
Raised Fist0
DSA Javaprogramming~30 mins

Activity Selection Problem in DSA Java - Build from Scratch

Choose your learning style10 modes available
Activity Selection Problem
📖 Scenario: You are organizing a conference with many talks. Each talk has a start time and an end time. You want to attend the maximum number of talks without any overlap.
🎯 Goal: Build a program that selects the maximum number of non-overlapping activities (talks) you can attend.
📋 What You'll Learn
Create an array of activities with given start and end times
Sort activities by their end times
Select the maximum number of activities that do not overlap
Print the selected activities in order
💡 Why This Matters
🌍 Real World
Scheduling meetings, talks, or tasks where you want to maximize usage of time without conflicts.
💼 Career
Common problem in software engineering interviews and useful in project management and operations research.
Progress0 / 4 steps
1
Create the activities array
Create an array of int[] called activities with these exact entries: {1, 4}, {3, 5}, {0, 6}, {5, 7}, {8, 9}, {5, 9}.
DSA Java
Hint

Use a 2D array where each element is an array of two integers: start and end time.

2
Sort activities by end time
Use Arrays.sort with a lambda comparator to sort activities by their end time (the second element in each sub-array).
DSA Java
Hint

Use Arrays.sort with a lambda expression comparing the second element of each activity.

3
Select maximum activities
Create an ArrayList<int[]> called selected. Add the first activity from activities to selected. Then use a for loop with variable i from 1 to activities.length - 1. Inside the loop, if the start time of activities[i] is greater or equal to the end time of the last selected activity, add activities[i] to selected.
DSA Java
Hint

Use an ArrayList to store selected activities. Compare start time of current activity with end time of last selected.

4
Print selected activities
Use a for loop with variable activity to iterate over selected. Inside the loop, print the start and end time of each activity in the format "Start: X, End: Y".
DSA Java
Hint

Loop over selected and print each activity's start and end times.

Practice

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

Solution

  1. Step 1: Understand the problem objective

    The Activity Selection Problem aims to choose the largest set of activities that do not overlap in time.
  2. Step 2: Identify the correct goal

    Among the options, only selecting the maximum number of non-overlapping activities matches the problem's goal.
  3. Final Answer:

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

    Goal = Max non-overlapping activities [OK]
Hint: Focus on maximizing non-overlapping activities [OK]
Common Mistakes:
  • Confusing longest duration with maximum count
  • Sorting by start time only
  • Summing all activity durations
2. Which of the following Java code snippets correctly sorts activities by their finish time for the Activity Selection Problem?
easy
A. Arrays.sort(activities, (a, b) -> a.finish - b.finish);
B. Arrays.sort(activities, (a, b) -> a.start - b.start);
C. Arrays.sort(activities, (a, b) -> b.finish - a.finish);
D. Arrays.sort(activities, (a, b) -> b.start - a.start);

Solution

  1. Step 1: Identify sorting criteria for Activity Selection

    Activities must be sorted by their finish time in ascending order to apply the greedy approach.
  2. Step 2: Match code to sorting by finish time ascending

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

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

    Sort by finish time ascending = Arrays.sort(activities, (a, b) -> a.finish - b.finish); [OK]
Hint: Sort activities by finish time ascending [OK]
Common Mistakes:
  • Sorting by start time instead of finish time
  • Sorting in descending order
  • Using incorrect comparator syntax
3. What is the output of the following Java code snippet for the Activity Selection Problem?
class Activity {
  int start, finish;
  Activity(int s, int f) { start = s; finish = f; }
}

Activity[] activities = {
  new Activity(1, 4),
  new Activity(3, 5),
  new Activity(0, 6),
  new Activity(5, 7),
  new Activity(8, 9),
  new Activity(5, 9)
};

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

int count = 1;
int lastFinish = activities[0].finish;
for (int i = 1; i < activities.length; i++) {
  if (activities[i].start >= lastFinish) {
    count++;
    lastFinish = activities[i].finish;
  }
}
System.out.println(count);
medium
A. 5
B. 3
C. 4
D. 6

Solution

  1. Step 1: Sort activities by finish time ascending

    The activities in sorted order (stable sort preserves order of ties): (1,4), (3,5), (0,6), (5,7), (8,9), (5,9)
  2. Step 2: Trace the greedy selection

    Pick (1,4), count=1, lastFinish=4
    Skip (3,5) since 3 < 4
    Skip (0,6) since 0 < 4
    Pick (5,7) since 5 >= 4, count=2, lastFinish=7
    Pick (8,9) since 8 >= 7, count=3, lastFinish=9
    Skip (5,9) since 5 < 9
  3. Step 3: Check if any other activities can be selected

    All activities considered, total count = 4 (including the first selected activity)
  4. Final Answer:

    4 -> Option C
  5. Quick Check:

    Greedy selection count = 4 [OK]
Hint: Sort by finish time, pick next starting after last finish [OK]
Common Mistakes:
  • Not sorting activities before selection
  • Selecting overlapping activities
  • Incorrectly updating last finish time
4. Identify the error in the following Java code snippet for the Activity Selection Problem:
Arrays.sort(activities, (a, b) -> a.finish + b.finish);

int count = 1;
int lastFinish = activities[0].finish;
for (int i = 1; i < activities.length; i++) {
  if (activities[i].start >= lastFinish) {
    count++;
    lastFinish = activities[i].finish;
  }
}
System.out.println(count);
medium
A. Loop starts from index 0 instead of 1
B. Comparator uses addition instead of subtraction for sorting
C. Condition should check start > lastFinish, not >= lastFinish
D. Count variable is not initialized

Solution

  1. Step 1: Analyze the comparator in Arrays.sort

    The comparator uses a.finish + b.finish, which does not correctly compare finish times. It should use subtraction (a.finish - b.finish) to sort ascending.
  2. Step 2: Understand impact of incorrect comparator

    Using addition returns a positive number regardless of order, causing incorrect sorting and wrong activity selection.
  3. Final Answer:

    Comparator uses addition instead of subtraction for sorting -> Option B
  4. Quick Check:

    Comparator must subtract finish times [OK]
Hint: Use subtraction in comparator, not addition [OK]
Common Mistakes:
  • Using addition in comparator instead of subtraction
  • Starting loop at wrong index
  • Misunderstanding condition operator
5. Given the following activities with start and finish times:
int[] start = {1, 3, 0, 5, 8, 5};
int[] finish = {2, 4, 6, 7, 9, 9};

Which set of activities selected by the greedy algorithm (selecting earliest finish time first) is correct?
hard
A. [Activity(1,2), Activity(3,4), Activity(5,7), Activity(8,9)]
B. [Activity(0,6), Activity(5,7), Activity(8,9)]
C. [Activity(1,2), Activity(5,9)]
D. [Activity(3,4), Activity(5,7), Activity(5,9)]

Solution

  1. Step 1: Sort activities by finish time ascending

    Activities sorted by finish time:
    (1,2), (3,4), (0,6), (5,7), (8,9), (5,9)
  2. Step 2: Select activities greedily

    Pick (1,2) first.
    Next, (3,4) start=3 >=2 select.
    (0,6) start=0 <4 skip.
    (5,7) start=5 >=4 select.
    (8,9) start=8 >=7 select.
    (5,9) start=5 <7 skip.
  3. Step 3: Final selected set

    [Activity(1,2), Activity(3,4), Activity(5,7), Activity(8,9)]
  4. Final Answer:

    [Activity(1,2), Activity(3,4), Activity(5,7), Activity(8,9)] -> Option A
  5. Quick Check:

    Greedy selection = [Activity(1,2), Activity(3,4), Activity(5,7), Activity(8,9)] [OK]
Hint: Pick activities with earliest finish time, skip overlapping [OK]
Common Mistakes:
  • Choosing activities with longer duration
  • Selecting overlapping activities
  • Ignoring sorting by finish time
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