In-Memory Driver Payments Aggregator
Goal
Design and implement an in-memory component that tracks driver payments and exposes one metric: the total cost of all deliveries (for a live dashboard).
APIs to Provide
-
addDriver(driverId: int, usdHourlyRate: double)
-
recordDelivery(driverId: int, startTime, endTime)
-
getTotalCost(): double
Requirements and Assumptions
-
Drivers have different hourly rates.
-
Drivers may have overlapping deliveries and are paid for each delivery independently.
-
Each delivery is recorded immediately after completion and lasts no more than 3 hours.
-
Time fields must support at least 1-second precision.
-
getTotalCost returns the aggregate cost across all drivers for a live dashboard.
-
All inputs are valid; exact output formatting is not required.
Clarifications to Make Explicit
-
Time representation: Use UTC Unix epoch seconds (int64/long) for startTime and endTime to avoid timezone/DST issues and meet the "≥ 1-second precision" requirement.
-
Currency arithmetic: Avoid accumulating money in double to prevent rounding drift on the live dashboard.
-
Thread-safety: Assume the component may be called concurrently (dashboard + event ingestion). Make the implementation thread-safe.