Python 3.11+
The examples use the standard Python syntax and containers supported by the browser Judge; the Sweep Line invariant is independent of a minor Python release.
Verify in Python docs(opens in a new tab)Sort boundary events and process them in order to track active intervals or geometric state. Learn its decision rule, maintained state, Python cost model, and transfer from a focused drill to a full Interview Problem.
Recognize it when
Consider Sweep Line when the prompt's constraints and required operations match this shape: Sort boundary events and process them in order to track active intervals or geometric state.

Sign in to save your code and progress across devices. The lesson and problem statement remain public.
A sweep line replaces continuous intervals with discrete start and end events. Between consecutive coordinates, the active state is constant.
Tie order encodes endpoint semantics. For half-open intervals, process ends before starts so intervals touching at one coordinate do not overlap; closed intervals may require the reverse.
def active_trace(intervals):
events=[]
for start,end in intervals:events.append((start,1));events.append((end,-1))
active=0;trace=[]
for coordinate,delta in sorted(events):active+=delta;trace.append([coordinate,delta,active])
return traceImplement active_trace(intervals). Intervals are half-open; process end before start at equal coordinates and return [coordinate,delta,active].
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Apply each event delta to the active count or data structure, then measure the quantity required by the contract. Group equal coordinates when intermediate same-coordinate states should not be observable.
def maximum_overlap(intervals):
events=[]
for start,end in intervals:events.append((start,1));events.append((end,-1))
active=best=0
for _,delta in sorted(events):active+=delta;best=max(best,active)
return bestImplement maximum_overlap(intervals). Return the maximum number of active half-open intervals.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Choose merging to compute the union of intervals. Choose a sweep line for concurrency, coverage counts, weighted activity, or geometry events. Sorting events costs O(n log n); the scan is linear unless active state uses a tree or heap.
Say: “Intervals become ordered boundary events, and active state describes the open span until the next coordinate. This tie order implements half-open semantics.” Explain when the answer is measured and how equal coordinates are grouped.
Python 3.11+
The examples use the standard Python syntax and containers supported by the browser Judge; the Sweep Line invariant is independent of a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| Sweep Line decision loop | O(n log n) | O(n log n) | Sweep sorted signed boundaries, relying on end-before-start tie order for half-open sessions. |
O(n) for the focused Find Peak Active Sessions implementation.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Applying the pattern without proving its ordering, monotonicity, or window invariant can silently miss valid candidates.
Prevent it: Write the decision rule beside the loop and verify it against Find Peak Active Sessions before optimizing.
Avoid
def peak_active_sessions(sessions):
passUse instead
def peak_active_sessions(sessions):
events = [(time, delta) for start, end in sessions for time, delta in ((start, 1), (end, -1))]
active = peak = 0
for _, delta in sorted(events):
active += delta
peak = max(peak, active)
return peakWhere you will hit this: Find Peak Active Sessions(opens in a new tab)
Processes starts before ends at equal timestamps and counts touching sessions as overlapping.
Prevent it: Use the public tests and preserve this state: Active state equals all events processed at the current sweep position.
Avoid
def peak_active_sessions(sessions):
events=[]
for s,e in sessions: events += [(s,1),(e,-1)]
active=peak=0
for _,d in sorted(events,key=lambda event:(event[0],-event[1])):
active+=d; peak=max(peak,active)
return peakUse instead
def peak_active_sessions(sessions):
events = [(time, delta) for start, end in sessions for time, delta in ((start, 1), (end, -1))]
active = peak = 0
for _, delta in sorted(events):
active += delta
peak = max(peak, active)
return peakWhere you will hit this: Find Peak Active Sessions(opens in a new tab)
Python slicing, copying, membership in lists, or repeated sorting inside the loop can invalidate the intended complexity.
Prevent it: Count every slice, copy, sort, membership check, and container update before claiming O(n log n).
Avoid
def peak_active_sessions(sessions):
events=[]
for s,e in sessions: events += [(s,1),(e,-1)]
active=peak=0
for _,d in sorted(events,key=lambda event:(event[0],-event[1])):
active+=d; peak=max(peak,active)
return peakUse instead
def peak_active_sessions(sessions):
events = [(time, delta) for start, end in sessions for time, delta in ((start, 1), (end, -1))]
active = peak = 0
for _, delta in sorted(events):
active += delta
peak = max(peak, active)
return peakWhere you will hit this: First and Last Target Position(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27
leetcode · checked 2026-07-26