Design an in-memory payroll system for a food delivery company.
Each driver has a unique driver_id and a fixed hourly_rate set when the driver is registered.
Implement a data structure that supports the following operations efficiently:
-
register_driver(driver_id, hourly_rate)
-
Add a new driver to the system.
-
record_shift(driver_id, start_ts, end_ts)
-
Record one completed shift for the driver.
-
start_ts
and
end_ts
are Unix timestamps in seconds, and
end_ts > start_ts
.
-
The pay for the shift is
hourly_rate * (end_ts - start_ts) / 3600
.
-
get_total_earned(driver_id)
-
Return the total earnings accumulated by that driver across all recorded shifts.
Follow-up:
-
settle_up_to(cutoff_ts)
-
Pay out all earnings from shifts that ended at or before
cutoff_ts
.
-
get_unpaid_balance(driver_id)
-
After one or more settlements, return how much money is still unpaid for that driver.
Discuss what data structures you would use, how each operation works, and the time and space complexity of your design. If a driver has many historical shifts, explain how to support the settlement operation efficiently.