Activity Selection Problem
A medium Greedy problem included in Apna College, Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Greedy
- Sheets
- 3
- Core for
- 3 roles
- Platform
- GeeksforGeeks
The problem
Given a collection of time intervals representing start and end times of activities, find the maximum number of activities that can be performed by a single person assuming one activity at a time.
Example 1
- Input
- start = [1,3,0,5,8,5], end = [2,4,6,7,9,9]
- Output
- 4
- Why
- Select activities [1,2], [3,4], [5,7], [8,9]. Maximum 4 non-overlapping activities.
Example 2
- Input
- start = [10,12,20], end = [20,25,30]
- Output
- 2
- Why
- Select [10,20] and [20,30], or [12,25] and [20,30]. Maximum 2.
Constraints
- 1 <= start.length == end.length <= 10^5
- 0 <= start[i] < end[i] <= 10^9
How to think about it
Updated 2026-09-09Sorting by start time or duration is a trap because an early or short activity can still run until midnight. The only metric that conserves future time is how early an activity finishes, because finishing as early as possible leaves maximum daylight for everything that follows.
Approaches, worst first
Exhaustive recursive subset
time O(2^n) · space O(n)
Branch on whether to include each activity, discarding branches that overlap. It checks every valid schedule to find the largest, which wastes time exploring subsets that have already fallen behind the greedy deadline.
Sort by finish timeWrite this one
time O(n log n) · space O(n)
Order activities by ascending end time. Walk through once, taking the next activity whenever its start time is greater than or equal to the previous selected end time. Sorting dominates the runtime, and the single scan guarantees the maximum count.
Where people lose marks · 3
- Sorting by start time instead of end time fails whenever a very long activity starts first and blocks several shorter ones.
- Strict inequality (`start > prev_end`) instead of `start >= prev_end` drops activities that touch boundaries, unless the specific problem statement forbids back-to-back usage.
- Pairing start[i] and end[i] must happen before sorting; sorting the two arrays independently breaks the association between starts and ends.
The theory behind it
Greedy — the ground this problem stands on. All Greedy problems
What Greedy is
A greedy algorithm makes the best-looking choice available right now, at every step, without ever looking back or second-guessing its decision. Think of a cashier making change by handing over the largest possible coin first, repeatedly, until the total is reached. Unlike dynamic programming, which saves and compares answers to multiple overlapping paths, a greedy strategy commits to one immediate option and keeps moving forward.
When to reach for it
Reach for greedy when problems ask for minimum jumps, interval scheduling, assigning resources to maximize satisfaction, or finding fractional values. Key signals include sorted orders where greedily taking the next item never hurts future options, or gas station round trips where running balances prove reachability. If you can prove that taking the immediate best choice never leaves you worse off than any alternative, greedy gives the fastest answer.
How the pattern works
Start by sorting the input to bring the most promising candidates to the front. At each position, evaluate your local rule, take the best available piece, and update your running state. The crucial mental step is proving the greedy choice property: demonstrate that picking this immediate winner cannot block a better global solution down the road. If choosing an item now forces you to reconsider past decisions when conditions change later, greedy fails and you must switch to dynamic programming instead.
What each operation costs
| Operation | Time |
|---|---|
| sort elements to enable greedy selection | O(n log n) |
| greedy single-pass scan through sorted input | O(n) |
| greedy choice using a priority queue | O(n log n) |
What usually goes wrong with Greedy
- Applying a greedy choice without proving it yields the global optimum, such as picking the largest coin first for arbitrary denominations where dynamic programming was required.
- Forgetting to sort the input before running the greedy loop, making local decisions on unordered elements that produce invalid answers.
- Picking items based on only one attribute when the optimal decision depends on a ratio or combination of multiple attributes.
Which roles need this problem
Greedy is a core topic for these 3 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 4 more roles, including Site Reliability Engineer, Search Engineer, Quant Developer.
Companies that have asked it
Tags taken from the problem's own GeeksforGeeks page — not a copied list.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Greedy problems
Problem set and role mapping as of .