Problems / Max Concurrent Sessions / Editorial
(start, +1)
(end, -1)
SUM(delta)
(event_time, delta)
delta
-1 < +1
MAX
The sweep line converts a question about intervals (which overlap whom?) into a question about deltas (when does the count change?), collapsing O(n²) pairwise comparison into sort + prefix sum. It's the same reduction operating theaters, meeting-room bookings, connection pools, and IP-range auditors use. And the subtlest line in the query is the humble tiebreaker: flip it to starts-first and every hand-off inflates the peak by one — a bug invisible on data without shared timestamps, which is why the dataset ships one.
Solve Max Concurrent Sessions yourself →