KatInterview

Amusement Park Tap-to-Ride: Credits, Surcharges, and Refunds

Data Structure DesignModel answer
HardModel answerData Structure Designsimulationpriority-queuestate-management

Last updated

Simulate a wristband payment system that drains expiring credits before a linked card, applies zone-based surcharges, and splits refunds back across both funding sources.

Amusement Park Tap-to-Ride: Credits, Surcharges, and Refunds

An amusement park lets guests load promotional ride credits onto their wristband — each credit balance has an expiry tick after which it becomes unusable. The wristband also has one linked payment card as a fallback. Your job is to implement the transaction engine that keeps every guest's balances accurate.

Function Signature

def process_events(events: list[str], zone_surcharges: list[str]) -> list[str]:

events is a time-ordered list of comma-separated records in one of three formats:

t,ADD_CREDIT,guest_id,card_id,amount,expires_at
t,RIDE,charge_id,guest_id,amount,zone_id
t,REFUND,charge_id,refund_amount

All monetary amounts are non-negative integers in cents. Each entry in zone_surcharges looks like "100-199:0.10", which means any ride whose zone_id falls in [100, 199] incurs a 10% surcharge on the portion billed to the linked card. Return one output string per input event, preserving order.

Event Rules

ADD_CREDIT — Adds or tops up a named credit balance for a guest. The credit is valid for any transaction where t < expires_at. Output: "".

RIDE — Charges the guest for a ride costing amount cents.

  • Draw first from the guest's valid, unexpired credit balances (those with balance > 0 and t < expires_at).
  • Prioritize the balance expiring soonest; break ties by lexicographically smaller card_id.
  • Any portion not covered by credits is billed to the linked card. The zone surcharge applies only to that card-billed portion; round half-cents up to the nearest whole cent.
  • Output: "{charge_id},{credit_used},{card_charged}".

REFUND — Returns up to refund_amount of the original pre-surcharge ticket price, capped at whatever hasn't already been refunded on this charge.

  • Draw the refund first from the card-billed base amount (the pre-surcharge portion charged to the card), reversing the surcharge at the same rate.
  • Any remaining refund beyond what was card-billed comes from the credit-paid portion and is returned as a brand-new, non-expiring store credit on the guest's wristband.
  • Output: "{charge_id},{card_refunded},{credit_refunded}".

Example

events = [
    "1,ADD_CREDIT,G1,C1,2000,100",
    "2,RIDE,R1,G1,5000,150",
    "3,REFUND,R1,1800",
]
zone_surcharges = ["100-199:0.10", "200-299:0.05"]

process_events(events, zone_surcharges)
# -> ["", "R1,2000,3300", "R1,1980,0"]

For the RIDE at t=2: the 2000-cent credit (zone 150, expires at t=100) has already expired — wait, t=2 < expires_at=100, so it is still valid. The full 2000 credit is consumed, leaving 3000 on the card. Zone 150 falls in [100-199] at 10%, so card charge = 3000 × 1.10 = 3300.

For the REFUND of 1800: the card-billed base is 3000, none of it has been refunded yet. 1800 comes entirely from that base, reversed at 10%: 1800 × 1.10 = 1980 back to the card. Nothing spills into the credit bucket, so credit_refunded = 0.

Constraints

  • Up to 10^5 total events.
  • zone_surcharges contains at most 100 non-overlapping ranges.
  • Every REFUND references a charge_id that appeared in a prior RIDE event.
  • Multiple REFUND events may target the same charge_id; the second one picks up where the first left off.

Hints

Hint 1

For each guest, think about what data structure keeps the 'soonest-expiring credit first' ordering efficient. You need to drain credits in that order on every RIDE.

Hint 2

The surcharge only applies to the card-billed portion, not the full ride price. Store the pre-surcharge card base and the rate separately per charge — you'll need them verbatim when computing refunds later.

Hint 3

On a REFUND, work bucket-by-bucket: first exhaust the unrefunded card-base (scaling the refund by 1+rate), then spill into the credit bucket and issue a fresh non-expiring balance. Track how much of the card-base has been refunded across multiple partial refunds.