KatInterview

Fleet Billing Ledger With Time-Dated Per-Minute Rates

Data Structure DesignAuto-graded · 14 test cases
MediumAuto-gradedData Structure Designdesignsimulationbinary-search

Last updated

Simulate a bike-fleet billing ledger where each ride is priced at the rate active at its start, with rate changes, duplicate ride ids and an exact total.

A bike-share company bills its e-bikes by the minute. You are given a batch of ledger commands and must replay them in order, returning one result per command.

Implement bike_fleet_billing(operations, arguments). operations[i] is the command name and arguments[i] is its argument list. The two lists have equal length.

Commands

CommandArgumentsMeaning
register_bike[bike_id, rate_per_minute]Adds a bike whose starting price is rate_per_minute dollars per minute. This rate applies to any ride that starts before the bike's first scheduled change.
update_rate[bike_id, new_rate, effective_at]Schedules a new per-minute rate for the bike, active for rides whose start time is at or after effective_at.
record_ride[ride_id, bike_id, start_time, end_time]Logs one finished ride and adds its charge to the running total.
get_total_cost[]Reports the running total of all charges so far.

Rules

  • Time units. Timestamps are integer Unix seconds. A ride lasts end_time - start_time seconds, which is (end_time - start_time) / 60 minutes. Partial minutes are billed proportionally, with no rounding up or down.
  • Which rate applies. A ride is priced at the rate in effect at its own start_time. That is the rate from the scheduled change with the largest effective_at that is <= start_time. If no such change exists, the bike's registration rate applies. The whole ride is charged at that single rate, even if another change would occur mid-ride.
  • Recording order does not matter for pricing. A ride recorded after later rate changes were scheduled is still priced by its own start_time, not by the bike's latest rate.
  • No repricing. A charge is fixed when record_ride is processed. Scheduling a change afterwards, even one with an effective_at earlier than rides already recorded, does not alter the running total. It only affects rides recorded later.
  • Same effective time. If several update_rate commands for one bike use the same effective_at, the most recently processed one wins.
  • Duplicate rides. ride_id values identify rides globally. A record_ride whose ride_id was already recorded is ignored completely and adds nothing. Only the first submission counts, even if its other arguments differ.
  • Exact money. Treat each rate as the decimal value it is written as (for example 0.2 means exactly 0.20). Keep the running total exact, without rounding individual charges. Only when answering get_total_cost, round the total to the nearest cent, with exact halves rounded up (away from zero). Format it with exactly two digits after the decimal point, such as "4.50" or "0.00".

Output

Return a list with one entry per command, in order. The entry is the formatted string for get_total_cost and None for every other command.

Constraints

  • 1 <= len(operations) == len(arguments)
  • Command names are always one of the four above, and argument lists have the shapes shown.
  • Every bike is registered (once) before any update_rate or record_ride refers to it.
  • 0 <= start_time <= end_time, all timestamps are integers, and a ride is shorter than one day.
  • Rates are non-negative numbers with at most a few decimal places.
  • Ids are strings.

Examples

Example 1

Input: bike_fleet_billing(['register_bike', 'record_ride', 'get_total_cost', 'update_rate', 'record_ride', 'get_total_cost'], [['bike1', 0.2], ['r1', 'bike1', 0, 600], [], ['bike1', 0.3, 600], ['r2', 'bike1', 600, 900], []])

Output: [None, None, '2.00', None, None, '3.50']

Example 2

Input: bike_fleet_billing(['register_bike', 'update_rate', 'record_ride', 'record_ride', 'get_total_cost'], [['bike1', 0.2], ['bike1', 0.3, 600], ['late', 'bike1', 0, 600], ['late', 'bike1', 600, 900], []])

Output: [None, None, None, None, '2.00']

Hints

Hint 1

For each bike, keep its registration rate plus a sorted list of (effective_at, rate) changes, instead of one mutable rate value.

Hint 2

To price a ride, binary search for the last change whose effective_at is <= start_time. If there is none, use the registration rate. When inserting a change with an equal effective_at, place it after existing equal ones so the newest wins.

Hint 3

Floating-point sums can drift and round wrongly at half-cents. Use exact decimal arithmetic (built from the string form of each rate), add up rate * seconds / 60 without rounding, and round once, half-up, when formatting the total.