Problem · Design

Delivery Cost Tracker

Learn this problem
MediumRipplingPHONE SCREEN

Problem statement

Build a cost tracker for delivery drivers. Each driver has a fixed hourly rate, and every recorded delivery contributes to the company's total delivery cost.

Operation Format

Process the rows of operations in order. Return one string for every query operation, in query order.

  • ["ADD_DRIVER", driverId, hourlyRate]: Add a driver whose hourly rate is a decimal USD string with at most two fractional digits. Driver IDs are unique.
  • ["RECORD_DELIVERY", driverId, startTime, endTime]: Record a delivery interval using integer Unix timestamps in seconds. The driver exists, startTime < endTime, and the interval lasts at most three hours.
  • ["GET_TOTAL_COST"]: Return the total cost of all recorded deliveries.
  • ["PAY_UP_TO", payTime]: Mark every delivery with endTime <= payTime as paid.
  • ["GET_UNPAID_COST"]: Return the total cost of all currently unpaid deliveries.

Cost Rules

  • A delivery's cost is hourlyRate * (endTime - startTime) / 3600.
  • Overlapping deliveries are paid independently, even when they belong to the same driver.
  • Format every returned cost with exactly two digits after the decimal point.
  • Every judged delivery produces an exact cent value, so no additional rounding rule is needed.

Interviewer follow-ups

Reports describe the task growing in stages: first maintain total cost, then support paying all completed deliveries up to a timestamp and querying unpaid cost. A later follow-up asks for the maximum number of simultaneously active drivers during the previous 24 hours. Interviewers also emphasized exact decimal handling for financial values.

Function

trackDeliveryCosts(operations: String[][]) → String[]

Examples

Example 1

operations = [["ADD_DRIVER","alice","10.00"],["RECORD_DELIVERY","alice","0","5400"],["GET_TOTAL_COST"],["GET_UNPAID_COST"],["PAY_UP_TO","5400"],["GET_UNPAID_COST"]]return = ["15.00","15.00","0.00"]

A 90-minute delivery at $10 per hour costs $15. It remains unpaid until PAY_UP_TO 5400.

Example 2

operations = [["ADD_DRIVER","a","18.00"],["ADD_DRIVER","b","24.00"],["RECORD_DELIVERY","a","100","1300"],["RECORD_DELIVERY","b","500","2300"],["GET_TOTAL_COST"],["PAY_UP_TO","1500"],["GET_UNPAID_COST"]]return = ["18.00","12.00"]

The overlapping deliveries cost $6 and $12 independently. Only the first has ended by timestamp 1500.

Constraints

  • 1 <= operations.length <= 100000
  • Timestamps are integer seconds.
  • Every judged delivery cost is an exact number of cents.

More Rippling problems

drafts saved locally
public String[] trackDeliveryCosts(String[][] operations) {
  // write your code here
}
operations[["ADD_DRIVER","alice","10.00"],["RECORD_DELIVERY","alice","0","5400"],["GET_TOTAL_COST"],["GET_UNPAID_COST"],["PAY_UP_TO","5400"],["GET_UNPAID_COST"]]
expected["15.00", "15.00", "0.00"]
checking account