Design and Implement a Driver Delivery Payroll Service
You are building a backend service to track delivery drivers' work intervals and compute wages. Implement the following functions and specify system details.
Functions to Implement
-
create_driver(base_rate)
-
Registers a driver with a base hourly rate.
-
record_delivery(driver_id, start_time, end_time)
-
Logs a delivery interval for a driver.
-
get_total_cost()
-
Returns the total cost across all drivers as the sum over deliveries of (driver_base_rate × delivery_duration).
-
pay_out_to_time(time)
-
Settles/marks wages as paid for all applicable deliveries up to the given timestamp.
-
remaining_cost_to_pay_out()
-
Returns the total unsettled wages after payouts.
Clarifications and Constraints
-
Time handling
-
Use UTC timestamps in seconds.
-
Intervals are half-open: [start_time, end_time), so duration = end_time − start_time.
-
Validate start_time < end_time.
-
Money and units
-
base_rate is hourly (e.g., dollars/hour). Convert correctly when multiplying by seconds.
-
Overlaps/back-to-back
-
Back-to-back delivery intervals for the same driver (end_time == next start_time) are allowed.
-
Specify how overlapping intervals for the same driver are handled.
-
Edits/cancellations and invalid inputs
-
Define how updates and cancellations are handled, especially after payouts.
-
Idempotency
-
Define guarantees for repeated API calls (e.g., retries).
-
Data models and persistence
-
Specify storage schemas/maps and how payout state is stored.
-
Performance
-
Provide expected time/space complexity for each function.
Provide and justify your design and implementation choices for the above.