Swap Two Numbers
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google reportedly sent this one in October 2025, and the first thing you'll notice is there's no brute force to rule out. Two integers in, a two-element array out. That's the whole problem. If you've been bracing for a graph or a DP table, relax a little. The OA is theater, and this is the warm-up act. Return [second, first] and you're done. The only real risk is overthinking it or tripping on the value range. If you freeze anyway, StealthCoder runs invisibly during the live assessment and hands you the answer as a safety net.
The problem
Given two integers first and second, return a two-element array containing their values after they are swapped. The returned array must be [second, first]. You may use ordinary temporary storage; no arithmetic-only or bitwise-only technique is required. Function swapNumbers(first: int, second: int) → int[] Examples Example 1 first = 7 second = -3 return = [-3,7] The second value becomes the first element, and the first value becomes the second element. Example 2 first = 5 second = 5 return = [5,5] Equal values remain equal after their positions are exchanged. Example 3 first = -1000000000 second = 1000000000 return = [1000000000,-1000000000] The operation simply reverses the two boundary values without arithmetic. Constraints -10^9 <= first <= 10^9 -10^9 <= second <= 10^9
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that there isn't one. Build a new array with the second value first and the first value second. No temp variable is even needed, because you're not mutating anything in place. The statement says ordinary temporary storage is fine, so skip the XOR and the add-subtract tricks. Those are the real pitfall. Summing two values near 10^9 can overflow a 32-bit int in some languages, and the arithmetic swap buys you nothing here. Plain construction is O(1) time and O(1) space. Check the equal-value case, 5 and 5, and the extreme case, -10^9 and 10^9. Both work with a direct return. If the live OA rattles you and you second-guess a trivial problem, StealthCoder sits invisibly on screen as a hedge. Honestly, you probably won't need it for this one.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Swap Two Numbers 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Swap Two Numbers FAQ
How hard is the Google Swap Two Numbers question really?+
It's about as easy as an OA question gets. You return [second, first]. There's no loop, no data structure, and no edge case beyond equal values. Spend your prep time on the harder problems in the same assessment, and don't burn minutes here.
What's the trick to Swap Two Numbers?+
There's no trick. Construct and return a new array with the values in reversed order. The problem explicitly says ordinary temporary storage is allowed, so you don't need XOR or arithmetic swapping. Simple and correct beats clever and risky.
Should I use XOR or add/subtract to swap?+
No. The statement doesn't require it, and the add/subtract version can overflow with values near 10^9 in fixed-width int languages. XOR works but adds nothing. Just return the two values in swapped order and move on.
Do equal values or negatives break anything?+
No. If both inputs are 5, you return [5,5], which is correct. Negatives and the extremes of -10^9 and 10^9 are fine because you never do arithmetic on them. Direct reordering handles every case in the constraints.
How do I prepare for this in 48 hours?+
Don't prep for this specific problem. It takes one line. Use your time on the other questions likely in the same Google OA, like arrays, hash maps, and graph traversal. Make sure you can write clean function signatures and return the right type quickly.