This question evaluates a candidate's ability to convert per-account net balances into a set of transfers, testing algorithmic problem solving, data-structure manipulation, and correctness under conservation invariants in transactional systems.
You are given a dictionary/map balances from accountId -> netBalance.
Produce a list of transfers that will settle all accounts to net 0.
Return a list of transfers like:
(fromAccount, toAccount, amount)
where
amount > 0
After applying all transfers:
0
.
Input:
{ A: -5, B: 2, C: 3 }
One valid output:
[(A, B, 2), (A, C, 3)]