Rebalance Bank Accounts to a Minimum Balance
Problem statement
Stripe tracks money across bank accounts and sometimes moves funds so that every account stays at or above a required minimum balance.
Each input row is accountName,balance. Return a working sequence of transfers in the form from,to,amount that leaves every account with at least threshold.
An optimal number of transfers is not required. For deterministic output, process underfunded accounts in input order and take funds from overfunded accounts in input order. Move as much as possible in each transfer without taking a donor below the threshold or raising the current receiver above it.
Interview follow-up: Minimum transfer count
The graded rebalanceAccounts method above remains unchanged. As an additional ungraded practice exercise, implement minimumTransfersAboveTarget(accounts, target), returning an integer minimum number of transfers. Use the same accountName,balance row representation, but require each final balance to be strictly greater than the variable integer target.
For this follow-up, assume 1 to 12 uniquely named accounts, nonnegative integer balances and target at most 1,000,000, and enough total money: sum(balance) >= accounts.length * (target + 1). Any two distinct accounts may exchange a positive integer amount without making a balance negative. Only the count is returned; the order of an optimal sequence is irrelevant. The interface, integer units, feasibility, direct-transfer rules and finite bounds are authored practice assumptions.
Because balances are integers, strictly greater than target means at least target + 1. This conversion applies to the follow-up only. The graded method still uses its inclusive threshold argument, its input-order greedy serialization, and its existing 500-account limit.
For accounts = ["a,8","b,7","c,13","d,12"] and target = 9, return 2. Send 2 from d to a and 3 from c to b; every account then has at least 10. The graded constructor with threshold 10 uses three input-order transfers and is still correct for its own task.
For accounts = ["a,0","b,2"] and target = 0, return 1: both accounts must finish at least 1. Returning zero would silently replace the strict target with an inclusive boundary.
Function
rebalanceAccounts(accounts: String[], threshold: long) → String[]Examples
Example 1
accounts = ["AU,80","US,140","MX,110","SG,120","FR,70"]threshold = 100return = ["US,AU,20","US,FR,20","MX,FR,10"]US first fills AU, then contributes its remaining surplus to FR. MX supplies FR's final ten.
Example 2
accounts = ["a,50","b,50","c,200"]threshold = 100return = ["c,a,50","c,b,50"]The only donor funds both receivers in their input order.
Constraints
1 <= accounts.length <= 500.- Account names are unique and contain no commas.
0 <= balance, threshold <= 10^12.- The total balance is at least
accounts.length * threshold. - All arithmetic fits in signed 64-bit integers.
- Ungraded minimum-count helper:
1 <= accounts.length <= 12, with the same unique, comma-free account row format. - Ungraded helper: integer
0 <= balance, target <= 1000000. - Ungraded helper feasibility:
sum(balance) >= accounts.length * (target + 1). - Ungraded helper: positive integer transfers may connect any two distinct accounts without negative balances; return only the minimum count.
- All helper bounds and transfer details are authored practice assumptions. The graded bounds above are unchanged.