← Questions

Refund allocation across prioritized payment methods

0
AirbnbCodingLast asked Apr 2026

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], where method is CREDIT, CREDIT_CARD, or PAYPAL.
  • Refund: [refund_id, "REFUND", payment_id, timestamp, amount], a refund already made against that payment.

All amounts are whole cents. Timestamps are integers.

Rules

  1. A payment can give back only what is left on it: its amount minus all earlier refunds against it.
  2. Refund by method first: all CREDIT payments, then CREDIT_CARD, then PAYPAL.
  3. Within one method, refund the most recent payment first. Break exact ties by payment id.
  4. Take as much as possible from each payment before moving to the next.
  5. 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 0 allowed, 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 for CREDIT, 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 REFUND rows 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

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding