Balancing Teams
Reported by candidates from Mathwork's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
You're up against Mathwork's Balancing Teams problem, reported November 2025. Two teams have skill arrays with empty slots (zeros), and you need to fill them so both teams have identical total skill, minimizing that sum. If it's impossible, return -1. The trap is thinking this is just about math, it's actually a constraint satisfaction problem where you need to figure out the minimum valid assignment, not brute force all possibilities.
The problem
Two teams have skill arrays teamA and teamB. A value of 0 means that player slot is empty. Fill every empty slot with a positive skill value no larger than 10000 so that both teams end with the same total skill. Return the minimum possible equal team sum, or -1 if no such assignment exists. Fill the zero in teamA with 1, and the zeros in teamB with 3 and 6. Both teams then sum to 20, which is the minimum equal sum.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The core insight is binary search on the target sum. For a given sum candidate, check if you can fill the zeros in both teams to reach it. Each empty slot in teamA needs a fill value between 1 and 10000; same for teamB. Calculate how much each team needs to reach the target, then verify it's achievable given the slot constraints. The trick most people miss: the minimum valid sum isn't random, it's constrained by the number of zeros and max fill value. Search from the current non-zero sum up to sum plus slots times 10000. StealthCoder can spot this pattern instantly if you blank on the binary search setup during the live OA.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Balancing Teams 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Mathwork's OA.
Mathwork reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Balancing Teams FAQ
Is this actually a greedy problem?+
No. Greedy fails because you can't just fill slots to minimize locally, you need both teams to match exactly. Binary search on the answer is the pattern. Each check is a feasibility test.
How do I know if a sum is achievable?+
For a target sum, calculate how much each team needs. If teamA needs N more skill and has Z empty slots, check if N is between Z (minimum, all 1s) and Z times 10000 (maximum). Do the same for teamB. Both must be achievable.
What's the search range for binary search?+
Lower bound is the current sum of all non-zero values across both teams. Upper bound is that sum plus the total empty slots times 10000. Most answers fall well below upper bound.
Can I just fill all zeros with 1?+
Only if both teams happen to sum to the same value afterward. In the example, filling teamA's zero with 1 works, but teamB's zeros need 3 and 6 to balance. Test it first.
What makes this hard in 60 minutes?+
Recognizing binary search is the pattern, not trying to enumerate assignments. Also, off-by-one errors in slot counting and forgetting that each zero can be filled independently with any value from 1 to 10000.