Compute transfers to balance account debts
Problem: Balance Accounts by Generating Transfers
You are provided with a mapping, balances, where each accountId is associated with its netBalance.
- A positive
netBalanceindicates that the account is owed money. - A negative
netBalanceindicates that the account owes money. - The sum of all
netBalancevalues across all accounts is guaranteed to be exactly zero.
Your task is to generate a list of financial transfers that will bring every account's netBalance to zero.
Output Format
The solution should return a list of transfers. Each transfer must be represented as a tuple or object (fromAccount, toAccount, amount), where amount must be a positive value.
Upon applying all generated transfers, every account's netBalance must be precisely 0.
Example
Input:
{ A: -5, B: 2, C: 3 }
One Valid Output:
[(A, B, 2), (A, C, 3)]
Constraints and Notes
- Any valid set of transfers that settles all accounts is acceptable.
- While correctness is the primary objective, solutions that achieve the settlement with a fewer number of transfers are preferred.
AI interview prep
Core dilemma
Efficiently settling all account debts and credits to zero by generating a minimal number of financial transfers, given a fixed set of initial balances.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Compute transfers to balance account debts
Problem: Balance Accounts by Generating Transfers
You are provided with a mapping, balances, where each accountId is associated with its netBalance.
- A positive
netBalanceindicates that the account is owed money. - A negative
netBalanceindicates that the account owes money. - The sum of all
netBalancevalues across all accounts is guaranteed to be exactly zero.
Your task is to generate a list of financial transfers that will bring every account's netBalance to zero.
Output Format
The solution should return a list of transfers. Each transfer must be represented as a tuple or object (fromAccount, toAccount, amount), where amount must be a positive value.
Upon applying all generated transfers, every account's netBalance must be precisely 0.
Example
Input:
{ A: -5, B: 2, C: 3 }
One Valid Output:
[(A, B, 2), (A, C, 3)]
Constraints and Notes
- Any valid set of transfers that settles all accounts is acceptable.
- While correctness is the primary objective, solutions that achieve the settlement with a fewer number of transfers are preferred.