Finding number of concurrent events given start and end times
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To find the maximum number of concurrent events, the standard solution is a sweep-line algorithm over sorted start and end times. The idea is simple: every start increases the number of active events, and every end decreases it. If you process those timeline changes in order, the peak active count is the answer.
Define Overlap Carefully First
Before writing code, decide whether an event ending at time t overlaps with another event starting at time t.
Two common conventions are:
- half-open intervals:
[start, end), where an end attdoes not overlap a start att - closed intervals:
[start, end], where they do overlap
Most scheduling problems use the half-open interpretation because it avoids double-counting boundary-touching events. Your sorting rule should match that choice.
Sweep-Line Approach
The efficient approach is:
- convert each start into a
+1event - convert each end into a
-1event - sort the timeline
- scan from left to right, updating the active count
- record the maximum active count seen
This avoids comparing every event with every other event.
Python Example
Here is a half-open interval implementation where end events are processed before start events at the same timestamp.
Because -1 sorts before +1 at the same timestamp, an event ending at 4 is removed before an event starting at 4 is counted. That matches half-open interval semantics.
Why This Is Efficient
The expensive step is sorting. If there are n events, you create 2n timeline markers and sort them in O(n log n) time. The scan itself is linear.
That is much better than the naive O(n^2) method of checking every pair of intervals for overlap.
For large scheduling inputs, the sweep-line method is the standard solution for a reason.
If You Need the Count at Every Time Segment
Sometimes the problem is not just the maximum overlap but the active count over each time segment. The same sweep-line structure still helps. Instead of only tracking the maximum, you can record the active count between consecutive timeline points.
That turns the algorithm into a compact way to build an occupancy timeline rather than just one summary number.
Heap-Based Alternative for Sorted Starts
Another common pattern is useful when events are already processed in start-time order. Sort by start time and keep a min-heap of current end times.
This also runs in O(n log n) and is often intuitive for meeting-room style problems.
Choose Based on the Exact Output You Need
Use the sweep-line event method when:
- you want a clean general solution
- you need to control boundary semantics precisely
- you may later need a full occupancy timeline
Use the heap method when:
- you mainly care about maximum overlap
- start-time ordering fits the problem naturally
Both are good. The sweep-line approach is slightly more explicit about interval-boundary rules.
Common Pitfalls
The most common mistake is skipping the overlap definition and then getting boundary cases wrong, especially when one event ends exactly as another starts.
Another mistake is trying to compare every pair of events for large inputs. That quickly becomes too slow.
Developers also forget that the sort order for equal timestamps changes the result. End-before-start and start-before-end represent different interval semantics.
Summary
- The standard efficient solution is a sweep-line algorithm over sorted start and end markers.
- Starts increase the active count, and ends decrease it.
- The sorting rule at equal timestamps must match your overlap definition.
- The algorithm runs in
O(n log n)time instead ofO(n^2). - A min-heap solution is also valid when you process events in start-time order and only need the maximum concurrency.

