cswork / libraryOpen practice workspace

Coding questions · xAI

Compute dasher pay from order events

This question evaluates a candidate's ability to process time-based event streams, manage overlapping active intervals, and compute aggregate payouts under concurrency constraints.

Dasher naive pay (active-time with overlapping orders)

You are given a list of events describing when a delivery driver ("Dasher") accepts and fulfills different orders.

Pay rules

  • Base rate: $0.30 per minute.
  • An order is active from the time it is ACCEPTed until it is FULFILLed.
  • Pay is multiplicative by concurrency: for each minute, the dasher earns

pay per minute=(#active orders during that minute)×0.30\text{pay per minute} = (\#\text{active orders during that minute}) \times 0.30

Input

events: a list of records (time, orderId, action) where:

  • time is an integer timestamp in minutes (e.g., minutes since midnight).
  • orderId is a string identifier.
  • action is one of: ACCEPT, FULFILL.

Output

Return the total pay as a double, rounded to two decimals.

Important details / assumptions

  • events may be unsorted; you must process them in increasing time.
  • If multiple events have the same time, process all ACCEPT actions before FULFILL actions at that same time.
  • Pay accrues over each interval between consecutive event timestamps. For an interval [t_i, t_{i+1}), the number of active orders is considered constant and equals the active set after applying all actions at t_i.
  • You may assume the input is valid: each order is accepted before it is fulfilled.

Example

Events:

  • 06:15 A ACCEPT
  • 06:18 B ACCEPT
  • 06:36 A FULFILL
  • 06:45 B FULFILL

Pay:

  • 06:15–06:18: 1 active × 3 min × 0.3 = 0.9
  • 06:18–06:36: 2 active × 18 min × 0.3 = 10.8
  • 06:36–06:45: 1 active × 9 min × 0.3 = 2.7

Total = 14.4

Compute Dasher Pay from Order Events

You are given a list of delivery events for a Dasher. Each event is a tuple (time, orderId, action), where action is either "ACCEPT" or "FULFILL". An order is active from the moment it is accepted until it is fulfilled. The Dasher earns $0.30 per minute for each active order, so overlapping orders increase pay multiplicatively by concurrency. The input list may be unsorted. Process events in increasing time order, and if multiple events happen at the same time, process all "ACCEPT" actions before all "FULFILL" actions. For each interval [t_i, t_{i+1}), the active order count is the count after applying all events at t_i. Return the total pay as a float rounded to two decimal places.

Constraints

  • 0 <= len(events) <= 200000
  • 0 <= time <= 10^9
  • action is either "ACCEPT" or "FULFILL"
  • The input is valid: every fulfilled order has a matching earlier or same-time accept when same-time actions are ordered as specified

Hints

  1. Sort the events by timestamp, and for equal timestamps make sure ACCEPT comes before FULFILL.
  2. Instead of adding floating-point pay each time, accumulate total active-order minutes first, then convert to dollars at the end.