KatInterview

Parcel Runner Payout Ledger with Peak Overlap Queries

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designsweep-linehash-mapintervals

Last updated

Simulate a stateful ledger driven by operation lists: track per-runner payout sums and compute maximum simultaneous half-open intervals clipped to a query window.

Parcel Runner Payout Ledger

A dispatch service keeps a ledger of parcel runs. Every run is carried out by one runner during a half-open time span [start, end): the runner is busy from start up to, but not including, end. Each run pays a fixed amount.

You are given a script of ledger operations and must simulate a fresh, empty ledger processing them in order. The function receives two parallel lists:

  • operations: a list of operation names.
  • arguments: a list where arguments[i] is the list of arguments for operations[i].

Three operations exist:

  • "record_delivery" with arguments [runner_id, start, end, pay]: add a run for runner_id over [start, end) that pays pay. Produces None.
  • "get_courier_total" with arguments [runner_id]: produces the sum of pay over all runs recorded so far for that runner, or 0 if the runner has none.
  • "get_peak_concurrent" with arguments [window_start, window_end]: considers all runs recorded so far, restricted to the window [window_start, window_end). Produces the largest number of runs that are simultaneously active at one single instant t with window_start <= t < window_end.

Details:

  • A run is active at instant t when start <= t < end. Because spans are half-open, a run ending at time x and another starting at x do not overlap.
  • Every run counts individually, even when several belong to the same runner or have identical spans.
  • If no run is active at any instant of the window, the peak is 0.

Return a list with one entry per operation, in order: None for record_delivery, and the integer result for the two query operations.

Constraints

  • 0 <= start < end <= 10^9
  • 0 <= window_start < window_end <= 10^9
  • 1 <= pay <= 10^5
  • runner_id consists of lowercase letters and digits, length 1 to 30.
  • At most 2,000 operations in total are record_delivery or get_peak_concurrent.
  • At most 10,000 operations are get_courier_total.
  • operations and arguments have equal length.

Examples

Example 1

Input: run_parcel_ledger(['record_delivery', 'record_delivery', 'record_delivery', 'get_courier_total', 'get_courier_total', 'get_peak_concurrent', 'get_peak_concurrent', 'get_peak_concurrent'], [['c1', 10, 30, 25], ['c2', 20, 50, 40], ['c1', 40, 60, 30], ['c1'], ['c3'], [15, 45], [30, 40], [70, 100]])

Output: [None, None, None, 55, 0, 2, 1, 0]

Example 2

Input: run_parcel_ledger(['record_delivery', 'record_delivery', 'get_peak_concurrent'], [['c1', 10, 20, 15], ['c2', 20, 30, 15], [0, 50]])

Output: [None, None, 1]

Hints

Hint 1

Handle the totals and the overlap queries separately: a dictionary from runner to running sum answers totals in constant time.

Hint 2

For the peak query, first clip each run to the window using max of starts and min of ends, and ignore runs whose clipped span is empty.

Hint 3

Turn each clipped span into a +1 event at its start and a -1 event at its end, sort, and sweep. Apply all events sharing a timestamp before updating the maximum so touching spans do not count as overlapping.

Hint 4

Timestamps reach 10^9, so do not allocate an array indexed by time; only work with actual endpoints.