time complexity
event scheduling
concurrent events
algorithm design
data structures

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 at t does not overlap a start at t
  • 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:

  1. convert each start into a +1 event
  2. convert each end into a -1 event
  3. sort the timeline
  4. scan from left to right, updating the active count
  5. 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.

python
1def max_concurrent_events(intervals):
2    timeline = []
3
4    for start, end in intervals:
5        timeline.append((start, 1))
6        timeline.append((end, -1))
7
8    timeline.sort(key=lambda x: (x[0], x[1]))
9
10    active = 0
11    best = 0
12
13    for _, delta in timeline:
14        active += delta
15        best = max(best, active)
16
17    return best
18
19
20intervals = [(1, 4), (2, 5), (4, 6), (3, 7)]
21print(max_concurrent_events(intervals))

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.

python
1import heapq
2
3
4def max_concurrent_with_heap(intervals):
5    intervals.sort()
6    heap = []
7    best = 0
8
9    for start, end in intervals:
10        while heap and heap[0] <= start:
11            heapq.heappop(heap)
12        heapq.heappush(heap, end)
13        best = max(best, len(heap))
14
15    return best
16
17
18intervals = [(1, 4), (2, 5), (4, 6), (3, 7)]
19print(max_concurrent_with_heap(intervals))

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 of O(n^2).
  • A min-heap solution is also valid when you process events in start-time order and only need the maximum concurrency.

Course illustration
Course illustration

All Rights Reserved.