Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
XOR swap: `a ^= b; b ^= a; a ^= b;`. Or arithmetic `a = a+b; b = a-b; a = a-b;` (risks overflow). In real code prefer a temp or tuple swap.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you know the trick AND its pitfalls/when to avoid it.
Where candidates slip
Ignoring the same-location aliasing bug or overflow.
How to stand out
Recommend a temp/tuple swap for clarity; reserve XOR for constrained contexts.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
XOR swap
1Hint 1Explore hintHide hint
XOR is its own inverse — can three XORs exchange two values?
2Hint 2Explore hintHide hint
What breaks if both operands are the same memory cell?
3Hint 3Explore hintHide hint
What would you actually ship in production?
A · Core idea
Swap two values without a third variable. The puzzle answers are the XOR swap (a^=b; b^=a; a^=b) and the arithmetic swap (add/subtract) — but the honest engineering answer is to just use a temp or tuple swap.
Why XOR swap works. XOR is its own inverse (x ^ x = 0) and commutative. The three chained XORs algebraically exchange the values: after a ^= b, a holds a⊕b; then b ^= a makes b = b⊕(a⊕b) = a; then a ^= b makes a = (a⊕b)⊕a = b.
The fatal aliasing bug. If the two operands are the same memory location (e.g. swap(arr[i], arr[i]) with i == j), XOR-swap zeroes the value (x ^ x = 0). A temp swap is immune. The arithmetic version can also overflow.
The senior signal. State that in real code clarity wins: a, b = b, a (or a temp) is preferred unless you're in a genuinely memory-constrained context. Knowing the trick and when not to use it is the point.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
a = 3, b = 7a ^= b
a = 3^7
a=4(=011^111)[7, 3]C · Code with teaching comments
D · Why it works and complexityO(1) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
The honest senior answer: clarity beats cleverness; a temp variable or tuple swap is preferred unless memory is truly constrained.
ComplexityO(1) time, O(1) space — a fixed number of operations regardless of input — constant time.
E · Edge cases and pitfalls5a = 0, b = 5 — Swaps to [5, 0].Explore edge casesHide edge cases
Edge cases handled
a = 0, b = 5Swaps to [5, 0].a = 4, b = 4Equal values stay [4, 4].same memory cellXOR-swap would zero it — use a temp.
Pitfalls
- XOR-swapping a variable with itself (same memory) zeroes it.
- Arithmetic swap overflowing for large values.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int[] swap(int a, int b) { a ^= b; b ^= a; a ^= b; return new int[]{a, b};Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- XOR-swapping a variable with itself (same memory) zeroes it.
- Arithmetic swap overflowing for large values.
Revise
Carry the pattern into the interview
Pattern to remember
XOR / arithmetic swap
Code pattern to memorize
a^=b; b^=a; a^=b (prefer: a,b = b,a)Rules to remember
- 1XOR is self-inverse.
- 2Three XORs swap.
- 3Prefer a temp in real code.
Use this pattern when
- In-place swap puzzles
Choose another pattern when
- Same memory location — XOR-swap zeroes it
Interview takeaway
XOR swap is a neat trick, but a temp variable is clearer.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: XOR / arithmetic swap. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups