KatInterview

Payroll From an Event Log With Surge Windows

Greedy & IntervalsModel answer
HardModel answerGreedy & Intervalsintervalsevent-processinghashing

Last updated

Turn a day's delivery events into total driver pay, then add doubled-rate surge windows by splitting intervals at boundaries, and decide how to handle messy or inconsistent logs.

A delivery platform records everything a driver does during one day as a list of JSON events. Each event has these fields:

  • order_id: identifies the delivery
  • event_type: either "start" or "complete"
  • timestamp: a numeric value in minutes (assume parsing and normalization are already done)
  • base_rate_per_minute: the pay rate for that delivery

A delivery is paid for the time between its start event and its completion event:

pay = (complete_time - start_time) * base_rate_per_minute

Part 1. Write a function that returns the driver's total pay for the day, summed over all deliveries.

Part 2 (follow-up). Some time ranges are flagged as surge windows, during which the rate is doubled. If a delivery only partly overlaps a surge window, the delivery must be split at each window boundary, and the doubled rate applies only to the pieces that fall inside a window.

Part 3. Explain how your solution behaves with dirty data: missing events, duplicates, events that are out of order, and impossible orderings.

Things to address in your answer:

  • The assumptions you would confirm before coding
  • The algorithm and its time and space complexity
  • How you treat overlapping surge windows, deliveries that touch a boundary exactly, and deliveries that run concurrently
  • Working code

Hints

Hint 1

Do not trust the order of lines in the log. Group the events by `order_id` first, then decide what each group represents by looking at event types and timestamps.

Hint 2

Reduce every order to one clean triple (start, end, rate), or reject it with a recorded reason. This validation step is where all the malformed-input rules live, so keep it separate from the pay arithmetic.

Hint 3

For the surge follow-up, sort and merge the surge windows into disjoint ranges first, so overlapping windows cannot stack the multiplier. Then walk each delivery from left to right, emitting segments at each boundary, and use binary search to find the first window that matters.