Calculate Transaction Fees
Company: Stripe
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: 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.
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.
Community answers
Answer by Maximus
Answer by prasadkirpekar96
Answer by anupamaixb04
Answer by ronsk0311