Compute a Merchant's Payout Reserve with Capped Rates and Per-Source Policies
Company: Stripe
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Technical Screen
A payments platform holds back part of the money a merchant processes as a reserve and pays out only the rest. Write the code that computes the reserve. The exercise has two parts, and the second changes the shape of the input. You may construct the input objects directly in code, so no JSON or other parsing is needed, and you are expected to write your own test cases. Both parts fit in a 45-minute coding interview.
### Clarifying Questions
- How should money be represented and rounded: dollars as decimals, integer cents, or a decimal type? What happens to a fraction of a cent?
- Can processing volume be zero or negative (for example after refunds), and what reserve should a negative amount produce?
- Can a policy be invalid, such as a rate above 100% or a negative cap, and should the function reject it?
- The merchant profile carries a timestamp that neither rule mentions. Can it be ignored?
### Part 1: Calculate a reserve for one merchant
A merchant processes some amount of money each day. Write `calculate_payout_reserve(merchant, policy)` that returns how much of that money should be held back from the merchant's payout.
- The merchant profile contains `current_processing_volume` and a numeric `timestamp`.
- The policy contains `reserve_rate_percent` and `max_reserve`. A rate of `5.0` means 5%.
Apply the rate to the processing volume, then cap the result at `max_reserve`. With a 5% rate and a \$1,500 cap:
| Processing volume | Reserve |
|---|---|
| \$15,000 | \$750 |
| \$35,000 | \$1,500 (5% would be \$1,750) |
```hint Money first
Decide how you will represent amounts before you multiply by a percentage, and what happens to a fraction of a cent.
```
#### What This Part Should Cover
- The rate applied first and the cap second, with the percentage converted correctly.
- A money representation that avoids binary floating-point error, and a stated rounding rule.
- Tests below, at and above the cap, and for zero volume.
### Part 2: Apply a separate policy to each source of funds
The merchant's `current_processing_volume` is now a map. Each entry gives how much money came from one source. The policy configuration also becomes a map, with a rate and a cap for each source:
| Source | Reserve rate | Maximum reserve |
|---|---|---|
| subscriptions | 3% | \$750 |
| advertising | 6% | \$1,200 |
| charity | 0% | \$0 |
| default | 5% | \$1,000 |
Return the merchant's total reserve. Apply the cap separately to each source before adding the amounts together. If a source, such as `sales`, has no named policy, use `default` for that source.
For example, a merchant with \$10,000 from subscriptions and \$2,000 from charity gets \$300 from subscriptions and \$0 from charity, so the function returns \$300.
```hint One calculation, many sources
Write the per-source calculation once, and decide what the Part 1 function becomes in terms of it.
```
#### What This Part Should Cover
- Each source's cap applied before summing, with the `default` fallback.
- Reuse of the Part 1 calculation instead of a copy of it.
- Behavior for an empty volume map, a missing `default` policy and an unrecognized source name.
### What a Strong Answer Covers
- Exact money arithmetic with an explicit rounding rule.
- One per-source function shared by both parts, with the Part 1 signature still working.
- Tests the candidate writes for each rule: cap binding and not binding, a zero-rate source, the default fallback.
- Validation of malformed policies, with clear errors.
### Follow-up Questions
- Policies change over time and each has an effective date. How would you use the profile's timestamp to choose the policy in force?
- A cap on the merchant's total reserve is added on top of the per-source caps. How does the order of capping change the result?
- How would you return a breakdown of the reserve by source so support staff can explain it to a merchant?
- Volumes arrive in several currencies. What changes in the data model and the arithmetic?
Overview: Write a function that computes how much of a merchant's processed volume is held back as a payout reserve, first with one capped percentage rate and then with a separate rate and cap for each source of funds plus a default policy. It tests careful money arithmetic, rule ordering and the test cases you write.
Read the full Stripe Software Engineer interview experience this question came from