Design: Delivery Cost System (Object-Oriented)
Goal
Build a class-based system to track delivery work by drivers, compute costs, manage payments, and answer a concurrency query.
Entities and Operations
Assume timestamps are epoch milliseconds (or a comparable monotonic time unit) and each driver has a fixed hourly rate. Costs are computed as:
-
cost = hourly_rate × duration_hours
-
duration_hours = (end_time − start_time) / 3600000
Implement a DeliveryCostSystem with the following methods:
-
registerDriver(driverId, hourlyRate)
-
Register a driver and their hourly rate.
-
recordDelivery(driverId, startTime, endTime)
-
Record a delivery interval for a driver.
-
getTotalCost() -> number
-
Return the total cost of all recorded deliveries (paid or unpaid).
-
payUpTo(timestamp)
-
Mark all deliveries ending on or before timestamp as paid.
-
getTotalUnpaid() -> number
-
Return the total unpaid cost of all recorded deliveries.
-
maxSimultaneousDriverInPast24Hours() -> integer
-
Return the maximum number of drivers that were simultaneously on a delivery within the last 24 hours relative to "now".
Clarifications (assume unless specified otherwise)
-
A driver may not have overlapping deliveries (at most one active delivery per driver at any time).
-
End times are exclusive for overlap checks (intervals are [start, end)).
-
If a delivery is recorded after a payUpTo that already covers its end time, it should be considered paid immediately.
-
Use currency-safe arithmetic (e.g., store cents as integers or use decimal types).
-
Single-threaded in-memory design is sufficient for this exercise.