Fair Candy Swap
A easy-tier problem at 63% community acceptance, tagged with Array, Hash Table, Binary Search. Reported in interviews at Odoo and 1 others.
Fair Candy Swap is the kind of problem that looks trivial until you code it wrong. You're given two arrays of candy prices, and you need to find a pair of candies (one from each array) such that swapping them makes both totals equal. Odoo and Swiggy have both asked it. With a 63% acceptance rate, most people solve it, but many miss the optimal path on their first try. If this hits your live assessment and the brute force approach times out, StealthCoder surfaces the right trick in seconds, invisible to the proctor.
Companies that ask "Fair Candy Swap"
Fair Candy Swap is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know.
Get StealthCoderThe naive approach is nested loops checking every pair, which works but isn't elegant. The real pattern emerges when you think about math first: if Alice has total A and Bob has total B, and they swap candy i from Alice's array with candy j from Bob's array, then Alice ends up with A - i + j and Bob with B - j + i. For fairness, A - i + j equals B - j + i, which simplifies to i - j = (A - B) / 2. So you compute the target difference, then hash one array and check if (candy + target) exists in the other. Hash Table or Binary Search on a sorted array both work. Common miss: forgetting that the difference must be an integer, or not recognizing this is a two-sum variant. StealthCoder handles the algebra and implementation instantly if you freeze on the mathematical insight.
Pattern tags
You know the problem.
Make sure you actually pass it.
Fair Candy Swap recycles across companies for a reason. It's easy-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Fair Candy Swap interview FAQ
Is this really an easy problem or do people usually get stuck?+
The 63% acceptance rate is honest. Most people brute force it and pass, but interviewers often push for the O(n) hash table solution. The mathematical insight (i - j = (A - B) / 2) isn't obvious without working through an example. Once you see it, the code is trivial.
Do I need to sort the arrays?+
No. Sorting helps if you use binary search, but hash table doesn't require it and is faster. Sorting is an optional approach, not mandatory. Pick hash table for O(n) time and move on.
What's the most common mistake candidates make?+
Forgetting to check that (A - B) is even. If the difference is odd, no integer solution exists, and you should return an empty result or handle it early. Also, nested loops work but are too slow for large inputs.
Why would Odoo and Swiggy ask this?+
It's a lightweight screen for algorithmic thinking and mathematical reasoning, not raw coding speed. It separates candidates who think before coding from those who just loop. Easy difficulty signals it's often an early-round filter.
Should I practice hash table and binary search both for this?+
Hash table is the intended solution and faster. Binary search on a sorted array is acceptable but slower (O(n log n)). Know hash table cold for this one. If you blank on the math during the OA, that's where StealthCoder closes the gap.
Want the actual problem statement? View "Fair Candy Swap" on LeetCode →