📖 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 overlaps.
🎯 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 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 talks, meetings, or tasks to maximize usage of time without conflicts.
💼 Career
Useful for roles in project management, event planning, and software development involving scheduling algorithms.
Use an array of objects with start and end properties.
2
Sort activities by their end time
Create a new array called sortedActivities by sorting activities in ascending order of their end property using activities.slice().sort((a, b) => a.end - b.end)
DSA Typescript
Hint
Use slice() to copy the array before sorting to keep original intact.
3
Select maximum number of non-overlapping activities
Create an array called selectedActivities and add the first activity from sortedActivities. Then use a for loop with variable i from 1 to sortedActivities.length. Inside the loop, if sortedActivities[i].start is greater or equal to the end of the last selected activity, add sortedActivities[i] to selectedActivities
DSA Typescript
Hint
Start with the first activity and add others only if they start after the last selected activity ends.
4
Print the selected activities
Use console.log to print the selectedActivities array
DSA Typescript
Hint
Print the selectedActivities array to see the chosen talks.
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
Step 1: Understand the problem goal
The Activity Selection Problem aims to choose activities that do not overlap and maximize the count.
Step 2: Identify the correct objective
Among the options, only selecting the maximum number of non-overlapping activities matches the problem's goal.
Final Answer:
Select the maximum number of activities that don't overlap in time -> Option A
Quick Check:
Goal = Max non-overlapping activities [OK]
Hint: Focus on maximizing count without time conflicts [OK]
Common Mistakes:
Confusing longest duration with maximum count
Sorting by start time only
Summing all activity durations
2. Which of the following TypeScript code snippets correctly sorts activities by their finish time for the Activity Selection Problem?
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) => b.start - a.start);
Solution
Step 1: Identify sorting criteria for Activity Selection
Activities must be sorted by their finish times in ascending order to apply the greedy approach.
Step 2: Match sorting code to criteria
activities.sort((a, b) => a.finish - b.finish); sorts activities by finish time ascending, which is correct for the problem.
Final Answer:
activities.sort((a, b) => a.finish - b.finish); -> Option A
Quick Check:
Sort by finish time ascending [OK]
Hint: Sort activities by finish time ascending [OK]
Common Mistakes:
Sorting by start time instead of finish time
Sorting finish time descending
Using incorrect comparison operators
3. What is the output of the following TypeScript code for the Activity Selection Problem?
{0,3} (last=3); skip {1,4}; {3,5} (last=5); {4,7} overlaps with {3,5} so skip; {5,9} (last=9); {8,10} overlaps with {5,9} so skip. Actually, {8,10} starts before 9, so overlaps. So selected are {0,3}, {3,5}, {5,9} only 3 activities.
Final Answer:
3 activities: {0,3}, {3,5}, {5,9} -> Option D
Quick Check:
Greedy picks non-overlapping max activities [OK]
Hint: Sort by finish time, pick next starting after last finish [OK]
Common Mistakes:
Selecting overlapping activities
Miscounting due to off-by-one errors
Ignoring sorting step
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.