Calculate Transaction Fees
Company: Stripe
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates data-structure and numerical computation skills, including mapping composite keys to fee rates, integer fixed-point arithmetic for fee calculation, and aggregation of per-transaction and total fees.
Constraints
- 0 <= len(fee_rules), len(transactions) <= 100000
- 0 <= amount_cents <= 10^9
- 0 <= rate_bps <= 10000
- Each `(payment_type, payment_status)` pair in `fee_rules` is unique
Examples
Input: ([("card", "paid", 290), ("card", "refunded", 100), ("bank_transfer", "paid", 80), ("wallet", "paid", 150)], [("tx1", "card", "paid", 10000), ("tx2", "card", "refunded", 10000), ("tx3", "bank_transfer", "paid", 25000), ("tx4", "wallet", "failed", 5000)])
Expected Output: ([290, 100, 200, 0], 590)
Explanation: The matching fees are 10000*290//10000 = 290, 10000*100//10000 = 100, 25000*80//10000 = 200, and no rule for `(wallet, failed)` so 0. Total = 590.
Input: ([("card", "paid", 290)], [])
Expected Output: ([], 0)
Explanation: There are no transactions, so the per-transaction fee list is empty and the total fee is 0.
Input: ([("card", "paid", 250)], [("a", "card", "failed", 1000), ("b", "card", "paid", 0)])
Expected Output: ([0, 0], 0)
Explanation: The first transaction has no matching rule, so its fee is 0. The second matches a rule, but the amount is 0, so the fee is also 0.
Input: ([("wallet", "paid", 333)], [("t1", "wallet", "paid", 999), ("t2", "wallet", "paid", 1)])
Expected Output: ([33, 0], 33)
Explanation: Using floor division: 999*333//10000 = 33 and 1*333//10000 = 0. The total is 33.
Hints
- A dictionary keyed by `(payment_type, payment_status)` lets you find the fee rate for each transaction in constant average time.
- You only need one pass over the transactions: compute each fee with integer division and keep a running total.