KatInterview

Ride-Share Wallet: Credit Vouchers, Zone Surcharges, and Refunds

Data Structure DesignModel answer
HardModel answerData Structure Designsimulationpriority-queueinteger-math

Last updated

Simulate a ride-share payment ledger that drains expiring vouchers in priority order, applies zone-based surcharges to card charges, and splits refunds between card and new store credit.

Ride-Share Wallet Ledger

A ride-share platform lets passengers pay for trips using ride credits (prepaid, promotional balances that expire) combined with a linked payment card for any shortfall. You need to build the ledger engine that processes a time-ordered stream of wallet events and produces one output line per event.

def process_ledger(events: list[str], surcharge_bands: list[str]) -> list[str]:

Event format

Each event is a |-delimited string. Timestamps are non-decreasing integers.

t|LOAD|rider|credit_id|amount|expires_at
t|TRIP|trip_id|rider|amount|zone
t|CANCEL|trip_id|amount

Each entry in surcharge_bands has the form lo-hi:bps. A trip whose integer zone falls in [lo, hi] incurs a surcharge of bps basis points (1 bps = 0.01 %) applied to the card portion only. All monetary values are integer cents.


Part 1 — Loading credits and paying for trips

A LOAD event adds a credit balance to a rider's wallet and outputs:

t LOAD <credit_id> <amount>

A TRIP event pays from the rider's available credits first, then puts any remainder on the card. Output:

t TRIP <trip_id> credits=<V> card=<C> surcharge=<S> charged=<C+S>

Part 2 — Expiry and spend priority

A credit is spendable only when t < expires_at (strictly less than). Spend credits in order of earliest expires_at first. Break ties by lexicographically smaller credit_id. A credit may be partially spent and retains its remaining balance.


Part 3 — Zone surcharges

S = round_half_up(C * bps / 10000)

Compute this with integer arithmetic only. When multiple bands cover the same zone, the first band listed in surcharge_bands applies. If no band matches, S = 0.


Part 4 — Cancellations and partial refunds

A CANCEL event returns money for a previous trip:

  • Refund the card first, up to the card portion not yet refunded. Surcharges are never refunded.
  • Any remainder is returned as a new credit for the same rider, up to the credit portion not yet refunded. The new credit's id is RF-<trip_id>-<k>, where k is the running refund count for that trip including this one. It expires at t + 90.
  • Output t CANCEL <trip_id> card=<X> credit=<Y>, appending voucher=<id> when Y > 0.
  • If the trip is unknown, or the requested amount exceeds what is still refundable, output t CANCEL <trip_id> REJECTED and change nothing.

Example

surcharge_bands = ["4000-4999:250", "9000-9499:175", "9100-9199:999"]

events:
10|LOAD|mia|c-b|1500|200
12|LOAD|mia|c-a|800|200
15|LOAD|mia|c-c|1000|50
20|TRIP|t1|mia|2000|4410
60|TRIP|t2|mia|3000|9120
70|CANCEL|t2|1500
75|TRIP|t3|mia|900|1000
80|CANCEL|t2|800
81|CANCEL|t9|100
100|TRIP|t4|mia|700|4000
110|CANCEL|t1|2500

Expected output:

10 LOAD c-b 1500
12 LOAD c-a 800
15 LOAD c-c 1000
20 TRIP t1 credits=2000 card=0 surcharge=0 charged=0
60 TRIP t2 credits=1300 card=1700 surcharge=30 charged=1730
70 CANCEL t2 card=1500 credit=0
75 TRIP t3 credits=0 card=900 surcharge=0 charged=900
80 CANCEL t2 card=200 credit=600 voucher=RF-t2-2
81 CANCEL t9 REJECTED
100 TRIP t4 credits=600 card=100 surcharge=3 charged=103
110 CANCEL t1 REJECTED

Walkthrough highlights:

  • t=20: c-c expires soonest (at 50), spent first. Then c-a beats c-b on the id tie-break. Total 1500 + 500 = 2000 from credits, card = 0.
  • t=60: zone 9120 matches both the 175 bps and 999 bps bands — first listed (175 bps) wins. round_half_up(1700 × 175 / 10000) = round_half_up(29.75) = 30.
  • t=80: second refund on t2; k=2 so new credit id is RF-t2-2.
  • t=100: the RF-t2-2 credit (600 cents, expires at 80+90=170) is now available and covers 600. Card base = 100; round_half_up(100 × 250 / 10000) = round_half_up(2.5) = 3.
  • t=110: only 0 card and 0 credits remain refundable on t1 → REJECTED.

Constraints

  • Up to 10⁵ events total.
  • All amounts are positive integers up to 10⁹ cents.
  • All credit_id and trip_id values are unique strings; preserve them exactly (do not normalise).

Hints

Hint 1

Model wallet state as a dict of credits per rider, each storing [balance, expires_at]. Sort usable credits by (expires_at, credit_id) before each trip — Python tuple ordering handles the tie-break for free.

Hint 2

Use only integer arithmetic for the surcharge: `(C * bps + 5000) // 10000` gives round-half-up without floating point. Float multiplication like `C * 0.0175` can produce values like 29.4999... in binary and round the wrong way.

Hint 3

For refunds, store card_left and credit_left per order as mutable counters. Always validate before mutating: reject first if amount > card_left + credit_left, then apply card-first logic and mint a new credit only if the credit portion is positive.