Prioritized Refund Allocation
AirbnbPhone screenMedium
A guest is owed a refund that must be repaid from the payments they made. Each payment is [payment_id, method, time, paid], where paid is the amount in cents and method is one of "CREDIT", "CREDIT_CARD" or "PAYPAL". Earlier refunds are given as [payment_id, amount] pairs; a payment can appear in several of them, and its remaining balance is paid minus its prior refunds, never below 0.
Part 1: Allocate the refund
Given the list of payments, the list of prior_refunds, and a refund amount in cents, spread refund over the payments and return the allocations.
Draw on payments in this order: method "CREDIT" first, then "CREDIT_CARD", then "PAYPAL". Within a method, the payment with the most recent time comes first; payments with the same method and time keep their input order. Use up one payment's remaining balance before moving to the next, so only the last payment used can contribute less than its balance. Skip payments whose remaining balance is 0.
If refund is larger than all remaining balances combined, refund only that total.
Return the allocations as [payment_id, method, amount] triples, in the order they are applied. Prior refunds do not appear in the output.
- Example 1
- refund
4500- payments
[["p1","PAYPAL",10,5000],["p2","CREDIT_CARD",20,3000],["p3","CREDIT",5,1000],["p4","CREDIT_CARD",30,2000]]- prior_refunds
[]- Output
[["p3","CREDIT",1000],["p4","CREDIT_CARD",2000],["p2","CREDIT_CARD",1500]]
Why: CREDIT comes first, so p3 gives its full 1000. Next is CREDIT_CARD, newest first: p4 (time 30) gives 2000. That leaves 1500, and p2 (time 20) gives 1500 of its 3000. PAYPAL is not touched.
Constraints
0 <= len(payments) <= 10^50 <= len(prior_refunds) <= 10^5payment_idvalues are distinct and1 <= len(payment_id) <= 101 <= time <= 10^90 <= paid <= 10^120 <= amount <= 10^12for each prior refund0 <= refund <= 10^12- Every prior refund names a payment in
payments methodis one of"CREDIT","CREDIT_CARD","PAYPAL"