The task started with basic gift card and credit card payments, then progressively introduced gift card expiration, regional surcharges, and refunds. The difficulty came from maintaining correct payment state across chronological events while handling increasingly complex business rules.
The assessment focused on a payment workflow similar to what merchants might use with Stripe Terminal. A customer can complete a purchase using multiple payment methods. For example, part of the bill might be covered by a gift card, while the remaining amount is charged to a credit card.
The system always attempts to use available gift card funds before charging the credit card. You are given a chronological sequence of payment events and must determine the correct payment result for each event.
Implement the following function process_events(events, surcharge_rates)
The surcharge_rates parameter contains non-overlapping ZIP code ranges and their corresponding processing fee percentages. For example: 94000-94999:0.04. This represents a 4% surcharge for ZIP codes within the specified inclusive range.
General Constraints
Events arrive in chronological order.
Monetary values are non-negative integers representing cents.
Every charge_id is unique.
Refunds always reference an earlier charge.
Each charge can be refunded at most once.
Gift card IDs are unique when initially added.
ZIP codes contain five numeric digits.
Surcharge ranges are valid and non-overlapping.
Surcharge calculations always produce whole cents, so rounding is unnecessary.
The function must return one output string per input event, preserving the original event order.
Part 1 — Basic Split-Tender Payments
The first part only includes ADD_GIFTCARD and CHARGE events. All customers begin with zero gift card balance.
ADD_GIFTCARD
When a gift card is added, its balance becomes available to the specified customer. The event returns an empty string.
CHARGE
When processing a purchase, the system first deducts as much as possible from the customer's gift card balance. If the gift card covers the entire purchase, the credit card charge is zero. Otherwise, the remaining unpaid amount is charged to the credit card. For Part 1, each customer has at most one active gift card, so there is no need to handle multiple-card prioritization.
Output Format
For each charge, return CC_CHARGE_AMOUNT,CARD_ID:AMOUNT_USED. If no gift card funds are used, return only the credit card amount.
The first purchase uses the entire 2700-cent gift card and charges the remaining 1400 cents to the credit card. The second purchase is fully covered by gift card funds.
Part 2 — Multiple Gift Cards and Expiration Dates
The second part introduces multiple gift cards per customer and expiration-based spending priorities. Each gift card maintains its own balance and expiration timestamp.
Expiration Rules
A gift card is valid for a charge occurring at timestamp T only when T < valid_before. Once the timestamp reaches or exceeds valid_before, that card expires and its remaining balance is permanently voided. Gift cards without expiration dates remain valid indefinitely.
**Gift Card Spending
Unlock Full Details
Create a free account to unlock this interview experience report.