Problem · Binary Search

Delivery Cost Tracker with Effective-Dated Rates

Learn this problem
HardRippling logoRipplingFULLTIMEPHONE SCREEN

Problem statement

Build a delivery-cost tracker whose driver rates can change at specified effective times.

Operation Format

Process the rows of operations in order. Driver identifiers and timestamps are encoded as decimal strings. Return one formatted cost for every query operation, in query order.

  • ["ADD_DRIVER", driverId, hourlyRate]: add a driver with an initial hourly rate effective from timestamp 0.
  • ["UPDATE_DRIVER_RATE", driverId, newRate, effectiveTime]: add an effective-dated rate version. If the same effective time is updated again, the later operation replaces the earlier version.
  • ["RECORD_DELIVERY", driverId, startTime, endTime]: record a delivery. Use the submitted rate version with the greatest effective time not after startTime. The delivery's cost is finalized now and is never revised by a later rate update.
  • ["GET_TOTAL_COST"]: return the total cost of every recorded delivery.
  • ["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 billed independently.
  • 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.

Function

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

Examples

Example 1

operations = [["ADD_DRIVER","1","12.00"],["RECORD_DELIVERY","1","0","1800"],["UPDATE_DRIVER_RATE","1","18.00","1000"],["RECORD_DELIVERY","1","1000","2000"],["GET_TOTAL_COST"],["GET_UNPAID_COST"]]return = ["11.00","11.00"]

The first delivery uses the initial $12.00 rate and costs $6.00. The second starts when the $18.00 rate is effective and costs $5.00. Both remain unpaid.

Example 2

operations = [["ADD_DRIVER","7","24.00"],["UPDATE_DRIVER_RATE","7","30.00","3600"],["RECORD_DELIVERY","7","0","1800"],["RECORD_DELIVERY","7","3600","7200"],["GET_TOTAL_COST"],["PAY_UP_TO","2000"],["GET_UNPAID_COST"]]return = ["42.00","30.00"]

The two deliveries cost $12.00 and $30.00. Paying through timestamp 2000 marks only the first delivery as paid.

Example 3

operations = [["ADD_DRIVER","3","10.00"],["UPDATE_DRIVER_RATE","3","20.00","100"],["UPDATE_DRIVER_RATE","3","30.00","100"],["RECORD_DELIVERY","3","100","460"],["GET_TOTAL_COST"]]return = ["3.00"]

The later update replaces the earlier rate at timestamp 100. A six-minute delivery at $30.00 per hour costs $3.00.

Constraints

  • 1 <= operations.length <= 100000.
  • 1 <= driverId <= 10^9, and every referenced driver has already been added.
  • Rates are USD decimal strings from 0.01 through 1000000.00 with at most two fractional digits.
  • 0 <= effectiveTime, startTime, endTime <= 10^9, startTime < endTime, and each delivery lasts at most 86400 seconds.
  • An effective rate exists at every recorded delivery's start time, and the total cost fits in a signed 64-bit integer number of cents.
  • Every judged delivery cost is an exact number of cents.

More Rippling problems

drafts saved locally
public String[] trackDeliveryCostsWithRateHistory(String[][] operations) {
  // Write your code here.
}
operations[["ADD_DRIVER","1","12.00"],["RECORD_DELIVERY","1","0","1800"],["UPDATE_DRIVER_RATE","1","18.00","1000"],["RECORD_DELIVERY","1","1000","2000"],["GET_TOTAL_COST"],["GET_UNPAID_COST"]]
expected["11.00", "11.00"]
checking account