This question evaluates a candidate's skills in designing efficient in-memory data structures and APIs for billing scenarios, including driver and delivery modeling, monetary representation, aggregation semantics, and performance characteristics.
You are asked to design an in-memory billing service for a food-delivery platform. The focus is on data structures and API behavior; you may ignore persistence, distribution, and thread-safety.
long
).
0 < (endTime - startTime) ≤ 3 * 3600
seconds (i.e., at most 3 hours).
For a single delivery, the payout is:
Example: for a driver with rate $10.00/hour, a delivery from time t to t + 5400 (1.5 hours) yields:
You may store money internally as integer cents to avoid floating point error, but the public API should return double.
Design the data structures and in-memory logic to implement the following methods:
addDriver(driverId: int, usdHourlyRate: double) -> void
driverId
and hourly rate.
driverId
is unique and the driver does not already exist.
recordDelivery(driverId: int, startTime: long, endTime: long) -> void
0 < endTime - startTime ≤ 3 * 3600
.
getTotalCost() -> double
Describe how you would design this in-memory service:
getTotalCost()
can run in amortized O(1) time, even as the number of deliveries grows large.
You do not need to provide code; focus on the design, data structures, time/space complexity, and how the core APIs would behave.
Login required