KatInterview

Peak Concurrent Couriers Over a Rolling 24-Hour Window

Data Structure DesignModel answer
HardModel answerData Structure Designsegment-treeintervalssliding-window

Last updated

Design a data structure that records courier delivery intervals and efficiently answers: what is the highest number of couriers simultaneously active at any instant in the last 24 hours?

Peak Concurrent Couriers Over a Rolling 24-Hour Window

A logistics platform tracks couriers in real time. Each time a courier picks up and completes a delivery, the system records a trip as (courierId, startTime, endTime) where times are integer Unix seconds.

You need to implement two operations on a TripTracker class:

record_trip(courier_id, start, end)   # called on every completed or active trip
peak_concurrent()                     # returns the answer described below

peak_concurrent() must return the maximum number of distinct couriers who were active at the same instant at any point within the window (now − 24 h, now].

What "active at instant t" means

A trip [s, e) covers instant t when s <= t < e. The interval is closed on the left, open on the right: a trip ending at time 10 and another starting at time 10 do not overlap. This matches real-world handoff semantics and avoids double-counting at boundaries.

What "distinct" means

If the same courier has two overlapping or back-to-back recorded trips, they still count as one courier at any instant they are both active. Your solution must not double-count.

What to return

Return a tuple (max_count, earliest_instant):

  • max_count — the peak number of simultaneously active couriers.
  • earliest_instant — the earliest second in the window where that peak is achieved.
  • If no trips fall inside the window, return (0, None).

Requirements

  • Describe your interval semantics and any assumptions before diving into code.
  • Analyze time and space complexity for both operations.
  • Handle edge cases: zero-length trips, trips partially outside the window, duplicate records for the same courier, and late-arriving records (inserts that are not in chronological order).
  • Discuss how you would handle trips that are still in progress (no end time yet).

Hints

Hint 1

Start by converting trips into timeline events (+1 at start, −1 at end) and think about what breaks when the same courier has overlapping records — the naive sweep overcounts. How would you fix that before even touching a data structure?

Hint 2

The query window is exactly 86,400 seconds wide, so the time axis is bounded. Think about a segment tree that supports two operations over an integer array: add a value to every index in a range, and query the maximum value (plus its leftmost position) over a range. Lazy propagation makes both O(log U).

Hint 3

Because you don't know timestamps in advance, a static segment tree won't work. Consider a dynamic (implicit) segment tree that allocates nodes only when a path is touched. Pair it with per-courier interval lists to enforce the distinctness rule: when recording a new trip, only increment the tree on the seconds not already covered by that courier.