Bank Account

Difficulty: MEDIUMCategory: DSAFrequency: 1Asked On 2025-06-02

Problem Statement

At Stripe, we keep track of where the money is — and move money between bank accounts to ensure their balances are not below some threshold. This is for operational and regulatory reasons, e.g., we should have enough funds to pay out to our users, and we are legally required to separate our users’ funds from our own.

This interview question is a simplified version of a real-world problem we have here.

Given the balances in different country accounts, can you move money between them so that they all have at least 100?

Note: We're not looking for the optimal solution, just a working one.

Initial Account Balances

•US: 148
•MX: 116
•SG: 120
•FR: 70

Sample Output (expected transactions):

•from: US, to: AU, amount: 28
•from: US, to: FR, amount: 26
•from: MX, to: FR, amount: 16


1def balance_accounts(accounts): 2 # Group accounts by their balances 3 deficit_accounts = {k: v for k, v in accounts.items() if v < 100} 4 surplus_accounts = {k: v for k, v in accounts.items() if v > 100} 5 transactions = [] 6 7 # Process the deficit accounts using a greedy approach 8 for deficit_acc, deficit_bal in deficit_accounts.items(): 9 deficit_amount = 100 - deficit_bal 10 11 # Try to cover the deficit from surplus accounts 12 for surplus_acc, surplus_bal in surplus_accounts.items(): 13 if surplus_bal > 100 and deficit_amount > 0: 14 transfer_amount = min(deficit_amount, surplus_bal - 100) 15 surplus_accounts[surplus_acc] -= transfer_amount 16 deficit_amount -= transfer_amount 17 18 transactions.append( 19 f"from: {surplus_acc}, to: {deficit_acc}, amount: {transfer_amount}" 20 ) 21 22 if deficit_amount <= 0: 23 break 24 25 return transactions 26

Solution

Comments