Refund allocation across prioritized payment methods
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Airbnb's coding round, last asked Apr 2026. It tests sorting, greedy and payments.
A customer's order was paid with one or more payments, and some of them may already have been partly refunded. Implement a function that splits a new refund across those payments.
def allocate_refund(transactions: list[list[str]], refund: int) -> list[tuple[str, int]]: ...
Each transaction is a list of strings in one of two shapes:
- Payment:
[payment_id, "PAYMENT", method, timestamp, amount], wheremethodisCREDIT,CREDIT_CARD, orPAYPAL. - Refund:
[refund_id, "REFUND", payment_id, timestamp, amount], a refund already made against that payment.
All amounts are whole cents. Timestamps are integers.
Rules
- A payment can give back only what is left on it: its amount minus all earlier refunds against it.
- Refund by method first: all
CREDITpayments, thenCREDIT_CARD, thenPAYPAL. - Within one method, refund the most recent payment first. Break exact ties by payment id.
- Take as much as possible from each payment before moving to the next.
- If the payments cannot cover the full refund, raise an error and allocate nothing.
Return a list of (payment_id, amount) pairs in the order the money is refunded. Skip payments that receive nothing.
Example
transactions = [
["a", "PAYMENT", "PAYPAL", "10", "4000"],
["b", "PAYMENT", "CREDIT_CARD", "20", "2500"],
["c", "PAYMENT", "CREDIT", "30", "800"],
["d", "PAYMENT", "CREDIT_CARD", "40", "1500"],
["x", "REFUND", "d", "50", "300"],
]
allocate_refund(transactions, 4000)
# -> [("c", 800), ("d", 1200), ("b", 2000)]
Credit c goes first. Card d is newer than b, but only 1200 is left on it after the earlier refund. The last 2000 comes from b, and PayPal is not touched. A request of 8501 or more raises an error, because only 8500 is refundable.
Constraints: up to 10^5 transactions; a refund always names a payment in the list, but it may appear before that payment.
Clarifying questions
- Are timestamps compared as integers (
"9"before"10"), not as strings? - In a tie on timestamp within one method, does the smaller payment id go first?
- What if earlier refunds against a payment already exceed its amount, or name an unknown payment?
- Is a refund of
0allowed, and what is the result: an empty list? - Should the earlier refunds count only if their timestamp is before now, or always?
What a strong answer covers
- First pass: collects payments; second pass: subtracts every refund from its payment's remaining amount, so a refund that appears before its payment still counts.
- Sorts payments by
(method rank, -timestamp, payment_id)with an explicit rank table forCREDIT,CREDIT_CARD,PAYPAL, and parses timestamps and amounts as integers. - Checks the total refundable first and raises before allocating anything, then takes
min(remaining, still_to_refund)from each payment, skipping zeros. - Keeps all money in integer cents,
O(t log t)for the sort. - Tests the example, a refund that exactly drains everything, an over-request, a fully refunded payment, and an unknown method.
Follow-ups
- Persist the new refund as
REFUNDrows so a second call sees it. - Add a method or change the priority order without code changes.
- Make the operation safe when two refunds for the same order arrive at the same time.
Where it's asked
- Airbnb · Coding · Last asked Apr 202645 min · Live coding, one or two rounds, from the same pool as the screen
What it tests
sorting · greedy · payments · simulation
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
