Skip to content
Hello Python

Checking your account…

Sign in to save your code and progress across devices. The lesson and problem statement remain public.

Loading the interactive Practice workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Practice workspace

Problem

Implement peak_active_sessions(sessions). Each session is half-open [start, end) with start < end. Return the maximum number active at once. A session ending at time t is inactive before another session starting at t.

Starter code

def peak_active_sessions(sessions):
    pass
Test cases

overlap-peak

{
  "args": [
    [
      [
        1,
        5
      ],
      [
        2,
        6
      ],
      [
        4,
        7
      ]
    ]
  ]
}

Expected: 3

touching-sessions

{
  "args": [
    [
      [
        1,
        3
      ],
      [
        3,
        5
      ],
      [
        5,
        8
      ]
    ]
  ]
}

Expected: 1

Wizard outline
  1. Step 1: Create signed events

    Establish activity for an empty or single-session sweep. The first checkpoint fixes the observable base before combining event streams.

  2. Step 2: Track the cumulative maximum

    Combine overlaps while leaving equal-time ordering for the final checkpoint. The prefix sum of signed events is the active count, but equal timestamps still need an explicit half-open rule.

  3. Step 3: Resolve equal timestamps

    Process ends before starts for half-open intervals. Tuple sorting with -1 before +1 applies the declared no-overlap boundary exactly.

Footguns and prerequisites
  • Processing starts first at the same timestamp invents an overlap.
  • Tracking only event timestamps without signed deltas loses direction.
  • arrays strings two pointers sliding window
Reviewed references
Recommended approach and implementation

Sweep sorted signed boundaries, relying on end-before-start tie order for half-open sessions.

Why it works: The running delta sum equals active sessions after each ordered boundary. Processing -1 before +1 at equal times removes ended sessions before adding new ones, matching the half-open contract.

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 peak