Prioritized Refund Allocation

Asked at Airbnb· Screen

Reported by the problem's creator, not verified

Import another question

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.

Examples

  1. Example 1

    Input: {"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]]

    Explanation: 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.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.