Transfers to bring every account up to a minimum balance
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Stripe's onsite coding (debug, integrate, build) round, last asked Nov 2023. It tests greedy, two pointers and backtracking.
You hold money in several bank accounts. Every account must end with at least a minimum balance. Given each account's balance, return transfers that move money from accounts above the minimum to accounts below it, without taking any donor below the minimum.
Part 1: any working set of transfers
Unless you agree on a different interface with the interviewer, assume:
-
rebalance(accounts: list[str], threshold: int) -> list[str]— accounts arename,balance; return transfers asfrom,to,amount. - For a fixed answer: handle the accounts below the threshold in input order, take money from donors in input order, and move as much as possible in each transfer without taking the donor below the threshold or raising the receiver above it.
rebalance(["AU,80", "US,140", "MX,110", "SG,120", "FR,70"], 100)
-> ["US,AU,20", "US,FR,20", "MX,FR,10"]
rebalance(["a,50", "b,50", "c,200"], 100)
-> ["c,a,50", "c,b,50"]
Part 2: the fewest transfers
-
min_transfers(accounts: list[str], target: int) -> int— every account must end strictly abovetarget, and any two accounts may exchange a positive amount. Return the smallest number of transfers.
min_transfers(["a,8", "b,7", "c,13", "d,12"], 9) -> 2 # d -> a 2, c -> b 3; all end at 10 or more
min_transfers(["a,0", "b,2"], 0) -> 1 # both need at least 1
The greedy order from Part 1 uses three transfers on the first case (with threshold 10); the minimum is two.
Constraints
- Part 1: up to 500 accounts; balances and threshold up to 10^12; the total is enough for every account.
- Part 2: up to 12 accounts; balances and target up to 10^6; the total is at least
n × (target + 1).
Clarifying questions
- Is the minimum inclusive (
>= threshold) or strict (> target)? - Is there always enough money in total? What should happen if not?
- Do transfers cost anything, so that fewer is better, or is any valid list fine?
- Can money pass through an intermediate account?
What a strong answer covers
- Part 1: compute each account's surplus or deficit, then a two-pointer pass over deficits and surpluses in input order; O(n) transfers.
- Part 2: the key fact that a group of k accounts whose surplus covers its deficits can be settled with k − 1 transfers, so the answer is n minus the largest number of disjoint self-sufficient groups.
- A bitmask DP over subsets (precompute each subset's net balance, O(3^n) or better) for n ≤ 12, and why greedy matching of largest donors to largest receivers is not always optimal.
- Accounts already above the target form groups of one and need no transfer.
- Tests: no transfers needed, one donor for many receivers, exact-threshold cases.
Follow-ups
- Return the actual transfers for the minimum, not just the count.
- Transfers between some pairs of accounts are not allowed (different banks or countries).
- Each transfer has a fee. Minimize total fees instead of count.
Where it's asked
- Stripe · Onsite coding: debug, integrate, build · Last asked Nov 202360 min · Project editor with starter code and tests: fix bugs in an unfamiliar codebase, wire an external API spec into a service, or complete a skeleton in parts · Varies by session: some onsites have one AI-assisted session and an integration session without AI
What it tests
greedy · two pointers · backtracking
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
