Magicsheet logo

Optimal Account Balancing

Hard
92.4%
Updated 6/1/2025

Optimal Account Balancing

What is this problem about?

The Optimal Account Balancing problem gives you a list of transactions between people. Find the minimum number of transactions needed to settle all debts so that everyone's net balance is zero. This hard coding problem reduces to matching creditors with debtors and counting the minimum number of settlements needed.

Why is this asked in interviews?

Uber, Goldman Sachs, Stripe, Amazon, Google, and many others ask this because it models real-world financial reconciliation. The bitmask DP or backtracking approach finds the minimum settlements by grouping people into zero-sum subsets. The array, bitmask, backtracking, bit manipulation, and dynamic programming interview pattern is the core.

Algorithmic pattern used

Debt balancing + backtracking/bitmask DP. Compute each person's net balance (positive = owed, negative = owes). Collect non-zero balances. Use backtracking: try to settle the first unsettled person with each later unsettled person (transferring debt). Count minimum transactions. Or use bitmask DP: dp[mask] = minimum transactions to zero out the subset mask. Transition: for each subset of mask summing to 0, dp[mask] = min(dp[subset] + dp[mask^subset]). Return dp[all_nonzero].

Example explanation

Balances: [+4,-2,-2]. Person 0 owed 4, persons 1,2 owe 2 each.

  • Settle person 1 with 0: 2 transactions... actually minimum: person 1 pays 0→2 txns, person 2 pays 0→2 txns = 2 transactions total. Or: person 1 pays person 0 (1 txn), person 2 pays person 0 (1 txn) = 2 transactions.

Common mistakes candidates make

  • Not computing net balances (raw transactions don't reveal minimum settlements).
  • Not filtering out zero-balance people (they don't need settlements).
  • Incorrect backtracking termination condition.
  • TLE without proper pruning or bitmask DP.

Interview preparation tip

Debt settlement problems reduce to "minimum number of zero-sum partitions of the balance array." For small n (≤10 people), bitmask DP over subsets summing to 0 works. For backtracking, always eliminate zero-balance people first. Practice similar "group settlement" problems in financial technology contexts — this exact problem appears in expense-splitting apps like Splitwise and is common in fintech interviews.

Similar Questions