Allocate refund across payments
Company: Airbnb
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: This question evaluates algorithm design and data-structure selection skills, focusing on greedy allocation, ordering by priority and recency, aggregation of transaction amounts, and analysis of time complexity.
Constraints
- 1 <= len(payments) <= 200000
- 0 <= refund <= 10^12
- For each payment: method ∈ {CREDIT, CREDIT_CARD, PAYPAL}
- For each payment: amount is an integer (0 <= amount <= 10^12)
- For each payment: ts is an integer UNIX timestamp (0 <= ts <= 10^12)
- Inputs are not pre-sorted
- If refund exceeds total available amount, refund only the available total
- Return allocations in the order they are applied
Hints
- Map payment methods to integer priorities (e.g., CREDIT:0, CREDIT_CARD:1, PAYPAL:2) and sort by (priority, -ts).
- Greedily consume each payment: allocate min(remaining_refund, payment_amount).
- Stop after the first partial allocation or when the refund is fully satisfied.
- Edge cases: refund = 0, no payments, or refund larger than total available.
- Use integers for amounts to avoid floating-point precision issues.