Reported July 2026
Salesforcebacktracking

Optimal Account Balancing

Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Salesforce OA. Under 2s to a working solution.
Founder's read

The Salesforce OA reported in July 2026 hands you Optimal Account Balancing, and the constraints are the whole story. Eight transactions, twelve people, amounts up to 100. That's tiny on purpose, because the intended answer is backtracking, not a clever greedy. Compute each person's net balance, drop the zeros, then search for the fewest transfers that zero everyone out. If you blank on the search, StealthCoder is the invisible safety net running on your screen during the live OA. Know the shape before you open the invite.

The problem

You are given an array transactions, where each row [from, to, amount] means that the person with identifier from gave amount units of money to the person with identifier to.
After all listed transactions, people may make additional transfers between any pair of accounts to settle every net balance.
Return the minimum number of additional transfers required so that every person's final net balance is zero.

Function
minTransfers(transactions: int[][]) → int

Examples
Example 1
transactions = [[0,1,10],[2,0,5]]
return = 2
Person 0 must receive a net amount of 5, person 1 must receive 10, and person 2 must pay 5. At least two non-zero accounts must receive money, and two transfers are sufficient.
Example 2
transactions = [[0,1,10],[1,0,1],[1,2,5],[2,0,5]]
return = 1
The final balances can be settled with one transfer of 4 from person 1 to person 0.
Example 3
transactions = [[0,1,10],[1,2,10],[2,0,10]]
return = 0
Every person's incoming and outgoing amounts already cancel, so no additional transfer is needed.

Constraints
1 <= transactions.length <= 8.
transactions[i].length == 3.
0 <= from, to < 12.
from != to.
1 <= amount <= 100.

Reported by candidates. Source: FastPrep

Pattern and pitfall

First step is easy: build a net balance per person, then throw away anyone at zero. What's left is a list of positive and negative numbers summing to zero. The trick is DFS over that list. Take the first nonzero balance, try pairing it with every later balance of opposite sign, settle the full amount into that person, recurse, then undo. The count of transfers is the depth. The pitfall is reaching for a pure greedy like matching largest debtor to largest creditor. It looks right and fails on cases where subsets cancel exactly. The small input size is your permission to explore. Prune by skipping duplicate balance values at the same level, and stop once the current count passes your best. If the recursion refuses to come together under pressure, StealthCoder can supply the working backtracking solution in real time during the live OA.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Optimal Account Balancing cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as optimal account balancing. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Salesforce's OA.

Salesforce reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Optimal Account Balancing FAQ

How hard is Optimal Account Balancing really?+

Hard by label, moderate in practice. The input is capped at 8 transactions and 12 people, so exponential backtracking is fine. The difficulty is seeing that the transaction history doesn't matter, only the net balances do. Once you reduce to a list of nonzero balances, it's a clean DFS.

What's the trick to solving it?+

Compute net balance per person, ignore zeros, then DFS. At each step fix the first unsettled person and try settling them with every later person holding the opposite sign. Recurse, backtrack, and track the minimum number of transfers found.

Why doesn't a simple greedy work?+

Matching the biggest debtor to the biggest creditor can miss groups of balances that cancel exactly with fewer transfers. A group summing to zero needs one fewer transfer than its size. Greedy can't see those groups, so you need to explore combinations.

Is the backtracking too slow for the constraints?+

No. After removing zero balances you have at most 12 people, usually fewer. Skip duplicate balance values at the same recursion level and break early when the current count can't beat the best. That's plenty for inputs this small.

How do I prepare for this in 48 hours?+

Write the net-balance step and the DFS from scratch twice. Test against the three examples, especially the one returning 0 and the one returning 1. Then practice the undo step, since forgetting to restore the balance is the most common bug.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Salesforce.

OA at Salesforce?
Invisible during screen share
Get it