KatInterview

Courier Earnings from Overlapping Order Events

Greedy & IntervalsAuto-graded · 20 test cases
MediumAuto-gradedGreedy & Intervalsinterval sweepevent sortingvalidation

Last updated

Sweep over unsorted start/end events to total concurrency-weighted seconds, validate malformed input, and convert to a half-up rounded dollar string.

Courier Earnings from Overlapping Order Events

A courier works several orders during one day. You are given a log of events, each marking when an order began or finished. Compute the courier's total earnings for the day.

Pay rule

The courier earns $0.30 per minute for every order that is in progress. If k orders are in progress at the same moment, the earning rate at that moment is k times the base rate. Two orders running at once therefore pay double during their overlap.

Pay accrues continuously by the second. One order-second is worth exactly half a cent. Add up the earnings for the whole day first, then round once to whole cents, rounding halves up.

Input

events is a list of dictionaries. Each dictionary describes one event and must have:

  • "orderId": an identifier for the order. It must not be None and must be hashable.
  • "type": either the string "START" or the string "END".
  • "timestamp": an integer number of seconds. bool values are not accepted.

Events may appear in any order.

Output

Return the total earnings as a string in dollars with exactly two decimal places, such as "0.30" or "12.05". Use no currency symbol and no thousands separators. An empty list yields "0.00".

Validity rules

An order is in progress from its START timestamp up to its END timestamp. Orders that share a timestamp contribute no paid time between those equal moments, and an order whose start and end are equal is valid and earns nothing.

Raise a ValueError (and return no amount) if any of the following holds:

  • An event is not a dictionary, or lacks any of the keys orderId, type, timestamp.
  • orderId is None or unhashable.
  • type is neither "START" nor "END".
  • timestamp is not an integer (including booleans).
  • An order has two START events or two END events.
  • An order is missing its START or its END.
  • An order's START timestamp is later than its END timestamp.

Constraints

  • Use exact integer arithmetic; do not use floating point for money.
  • Rounding happens only once, on the final total, and halves round up.
  • Timestamps are integers and may be any size that fits in a Python int.
  • The number of events can be large, so aim for roughly O(n log n) time.

Examples

Example 1

Input: compute_courier_earnings([{'orderId': 'A', 'type': 'START', 'timestamp': 0}, {'orderId': 'A', 'type': 'END', 'timestamp': 60}])

Output: '0.30'

Example 2

Input: compute_courier_earnings([{'orderId': 'B', 'type': 'END', 'timestamp': 120}, {'orderId': 'A', 'type': 'START', 'timestamp': 0}, {'orderId': 'B', 'type': 'START', 'timestamp': 30}, {'orderId': 'A', 'type': 'END', 'timestamp': 90}])

Output: '0.90'

Example 3

Input: compute_courier_earnings([{'orderId': 'A', 'type': 'START', 'timestamp': 0}, {'orderId': 'A', 'type': 'END', 'timestamp': 1}, {'orderId': 'B', 'type': 'START', 'timestamp': 1}, {'orderId': 'B', 'type': 'END', 'timestamp': 2}])

Output: '0.01'

Hints

Hint 1

Group events by order ID first so you can verify each order has exactly one START and one END, in a sensible order.

Hint 2

Turn each order into two changes on a timeline: +1 concurrent order at its start time and -1 at its end time. Merge changes sharing the same timestamp and sweep the sorted timestamps.

Hint 3

Between consecutive timestamps, add (time gap) * (current active count) to a running total of order-seconds. Each order-second is worth half a cent, so cents = ceil(total / 2), which is (total + 1) // 2 in integers.