Activity Selection by Earliest Finish
Schedule the most non-overlapping events.
Activity Selection by Earliest Finish is a free Coding Interview Prep lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Scheduling Problem
Given events with start and end times, activity selection asks for the most events you can attend without any two overlapping. 📅
Overlap Means Conflict
Two activities clash if one starts before the other ends. You can pick only one event from any overlapping pair.
The Winning Rule
The greedy key is to always pick the event that finishes earliest among those still available. Ending soon leaves the most room for others.
Sort by Finish Time
Start by sorting all activities by their end time. Now the best next choice is simply the next one in this order that fits.
events.sort(key=lambda e: e[1])Track the Last End
Keep one variable for the last chosen finish time. Any new event must start at or after this value to be compatible.
last_end = -1Sweep and Select
Walk the sorted list once. If an event starts at or after last_end, take it and update last_end to its finish.
for s, f in events:
if s >= last_end:
count += 1
last_end = fIt Runs in n log n
The cost is the sort, O(n log n), then a single linear sweep. That is fast enough even for very large contest inputs.
Why Earliest Finish Wins
Finishing first frees the timeline soonest, so it can never block a better plan. Swapping it into any optimal schedule keeps it just as good.
Earliest Start Fails
Picking by earliest start can grab one long event that hogs the whole day. Duration alone misleads you too, so trust the finish time.
Handle Boundary Touches
Decide if an event ending exactly when another begins counts as a clash. Use s >= last_end to allow back-to-back events.
A Common Contest Skin
This pattern hides behind many tasks: booking rooms, watching shows, or running jobs. Spot it and the earliest-finish rule applies.
Quick Check
You want the maximum number of non-overlapping activities.
Recap
Sort activities by finish time, then take each one that starts after your last pick ends. One sort plus one sweep gives the maximum set. 🚀
Frequently asked questions
Is the “Activity Selection by Earliest Finish” lesson free?
Yes — the full text of “Activity Selection by Earliest Finish” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.
What will I learn in “Activity Selection by Earliest Finish”?
Schedule the most non-overlapping events. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Coding Interview Prep?
No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Activity Selection by Earliest Finish” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Coding Interview Prep lesson?
Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Greedy Mindset
- Activity Selection by Earliest Finish
- Fractional Knapsack by Ratio
- Spot When Greedy Fails