Design a payment ledger with idempotent payments and partial refunds
Company: Stripe
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a `PaymentLedger` class that records and manages financial transactions for multiple users. The ledger supports adding payments with idempotent payment IDs, processing partial refunds against existing payments, and calculating the total balance for a given user.
A payment adds to a user's balance. A refund subtracts from a specific payment. The total refunded against a payment cannot exceed the original payment amount. If a refund would exceed the remaining refundable amount, only the remaining amount is refunded.
Payment IDs are globally unique identifiers. If a payment is added with a payment ID that already exists, the call is ignored, which makes retried calls idempotent.
Implement the following methods:
- `addPayment(userId: str, paymentId: str, amount: int) -> bool`. Record a payment of the given amount for the specified user. If a payment with the same `paymentId` already exists, ignore the call and return `False`. Return `True` if the payment was added.
- `refund(paymentId: str, refundAmount: int) -> int`. Process a partial refund against an existing payment. If `paymentId` does not exist, return 0. If `refundAmount` exceeds the remaining refundable amount (the original amount minus everything already refunded), refund only the remaining amount. Return the amount actually refunded.
- `getBalance(userId: str) -> int`. Return the user's total balance: the sum of all their payment amounts minus all refunds processed against those payments. Return 0 if the user has no payments.
The first example reuses the ID `p1` for a different user, which is ignored, and then refunds `p1` until nothing is left:
```text
Input:
["PaymentLedger", "addPayment", "addPayment", "addPayment", "refund", "refund", "refund", "getBalance", "getBalance"]
[[], ["alice", "p1", 500], ["alice", "p2", 300], ["bob", "p1", 900], ["p1", 200], ["p1", 400], ["p1", 50], ["alice"], ["bob"]]
Output: [null, true, true, false, 200, 300, 0, 300, 0]
Explanation:
ledger = PaymentLedger() # initialize empty ledger
ledger.addPayment("alice", "p1", 500) # -> True (alice: p1=500)
ledger.addPayment("alice", "p2", 300) # -> True (alice: p2=300)
ledger.addPayment("bob", "p1", 900) # -> False (p1 already exists, call ignored)
ledger.refund("p1", 200) # -> 200 (p1 had 500 refundable, 300 left)
ledger.refund("p1", 400) # -> 300 (capped at the remaining 300)
ledger.refund("p1", 50) # -> 0 (p1 is fully refunded)
ledger.getBalance("alice") # -> 300 (0 left on p1 + 300 on p2)
ledger.getBalance("bob") # -> 0 (bob has no payments)
```
The second example shows that a refund against an unknown payment ID returns 0 and changes nothing:
```text
Input:
["PaymentLedger", "addPayment", "addPayment", "getBalance", "getBalance", "refund", "getBalance"]
[[], ["bob", "p1", 1000], ["carol", "p2", 500], ["bob"], ["carol"], ["p3", 100], ["bob"]]
Output: [null, true, true, 1000, 500, 0, 1000]
Explanation:
ledger = PaymentLedger() # initialize empty ledger
ledger.addPayment("bob", "p1", 1000) # -> True (bob: p1=1000)
ledger.addPayment("carol", "p2", 500) # -> True (carol: p2=500)
ledger.getBalance("bob") # -> 1000
ledger.getBalance("carol") # -> 500
ledger.refund("p3", 100) # -> 0 (p3 does not exist)
ledger.getBalance("bob") # -> 1000 (unchanged)
```
```hint Lookup by payment ID
Two of the three methods start from a payment ID. Pick a structure with constant-time lookup by that ID, and decide what each entry has to remember so later refunds can be checked.
```
```hint The refund cap
The cap applies to each payment separately, and `refund` returns what was actually refunded. Trace one payment that receives several refunds until nothing is left.
```
### Constraints & Assumptions
- `1 <= userId.length <= 50`
- `1 <= paymentId.length <= 50`
- `1 <= amount <= 10^6`
- `1 <= refundAmount <= 10^6`
- At most `10^4` total calls will be made.
- Payment IDs are globally unique across all users.
### Clarifying Questions to Ask
- If a repeated payment ID arrives with a different user or a different amount, should it still be ignored silently, or reported as a conflict?
- Are amounts integers in the smallest currency unit, and is there only one currency?
- How often is `getBalance` called compared with `addPayment` and `refund`?
- Is the ledger used from a single thread, or must the methods be safe under concurrent calls?
### What a Strong Answer Covers
- What each stored payment record must hold so the idempotency check and the refund cap both work in constant time.
- How `refund` behaves for an unknown payment, a partial refund, an over-refund and a payment that is already fully refunded.
- How `getBalance` finds a user's payments, and the cost of that choice compared with keeping a running total.
- Time and space complexity of each method.
- Edge cases such as a payment ID reused by another user, a user with no payments, and totals that do not fit in a 32-bit integer.
### Follow-up Questions
- How would you make refunds idempotent, so a retried refund request is not applied twice?
- How would the design change if many threads or servers call the ledger at once, or if the data lives in a database?
- How would you keep a full history of payments and refunds and recompute any balance from it?
- How would you support payments in several currencies?
Overview: Design a PaymentLedger class that records payments under globally unique IDs, ignores repeated IDs, caps partial refunds at the remaining refundable amount, and reports each user's balance. It tests hash map design, idempotency and edge cases such as over-refunds and unknown payment IDs.