Rebalance Bank Accounts to a Minimum Balance
Updated
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. To get the same answer every time, handle the underfunded accounts in input order and take funds from the 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.
Follow-up: Minimum transfer count
The rebalanceAccounts method above stays the same. As an extra exercise that the tests do not check, 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.
Because balances are integers, strictly greater than target means at least target + 1. This conversion applies to the follow-up only. The main method still uses its inclusive threshold argument, its input-order greedy order of transfers, 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 main method with threshold 10 uses three input-order transfers, and that 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"]Every account needs at least the threshold. AU is short by twenty and FR by thirty. US has forty extra, so it first fills AU with twenty, then gives its remaining twenty to FR. FR still needs ten, and MX supplies it. So US sends twenty to AU, US sends twenty to FR, and MX sends ten to FR.
Example 2
accounts = ["a,50","b,50","c,200"]threshold = 100return = ["c,a,50","c,b,50"]Accounts a and b are each fifty short, and c has a hundred extra. So c sends fifty to a, then fifty to b, in 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.
- Extra exercise, minimum count:
1 <= accounts.length <= 12, with the same unique, comma-free account row format. - Extra exercise: integer
0 <= balance, target <= 1000000. - Extra exercise, enough money:
sum(balance) >= accounts.length * (target + 1). - Extra exercise: positive integer transfers may connect any two distinct accounts without negative balances; return only the minimum count.
- The bounds and transfer details of the extra exercise are as stated in the follow-up. The main bounds above are unchanged.
More Stripe Onsite Interview questions
- Invoice / Payment ReconciliationPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Parse Boolean Rule ExpressionsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Render a Task Tree from CSV RowsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Transaction Rule Expression EvaluatorONSITE INTERVIEW · Seen Oct 2026
- Evaluate Ordered Action RulesPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- WebSocket Load Balancer — Parts 1–5OA · ONSITE INTERVIEW · Seen Sep 2026
- Rule-Driven Transaction Fraud DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Account Balance Manager Part 3 - Platform CoverageONSITE INTERVIEW · Seen May 2026
- Account Balance Manager Part 2 - Reject OverdraftsONSITE INTERVIEW · Seen May 2026
- Account Balance Manager Part 1 - Calculate TotalsONSITE INTERVIEW · Seen May 2026
Similar questions at other companies
- Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
- Container With Most WaterAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Assign Cookies With Matching ParityInfosys · ONSITE INTERVIEWInfosys interview questions
- Container With Most WatereBay · ONSITE INTERVIEWeBay interview questions
- Most Water Between Two LinesExpedia · ONSITE INTERVIEWExpedia interview questions
- Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
- Container With Most WaterEPAM · PHONE SCREENEPAM interview questions
- Latest Arrival to Catch a BusAutodesk · OAAutodesk interview questions
- Container With Most WaterNuro · PHONE SCREENNuro interview questions
- Minimum Last-Element RelocationsChubb · OAChubb interview questions