Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Two pointers — `left = 0`, `right = n-1`. Swap `nums[left]` and `nums[right]`, then move both toward the centre until they cross. O(1) extra space.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you reverse in place with two pointers and get the loop bound right.
Where candidates slip
Swapping across the whole array (double swap) or an out-of-bounds right pointer.
How to stand out
Note the same primitive powers reverse-string and the rotation-by-three-reversals trick.
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
Two-pointer swap
1Hint 1Explore hintHide hint
Pair the first element with the last, second with second-last…
2Hint 2Explore hintHide hint
Two pointers from both ends; swap and move inward.
3Hint 3Explore hintHide hint
Stop when left meets right — use `<`, not `<=`.
A · Core idea
Reverse in place with the two-pointer swap: left at the start, right at the end, swap them, and step both inward until they meet. O(n) time, O(1) extra space — no second array.
Why pointers converge in the middle. Each swap places two elements in their final positions, so after n/2 swaps every element is reversed. The middle element of an odd-length array is already in place and needs no swap — which is exactly why the loop condition is left < right (strictly), not <=.
Why `<=` would be a bug. With left <= right, the middle element would be swapped with itself (harmless), but more importantly on even lengths the pointers would cross and you'd swap the same pairs back, un-reversing the array. left < right stops cleanly when they meet or cross.
This swap-and-converge primitive underlies reverse-string and the three-reversal array rotation trick.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [1, 2, 3, 4]l=0,r=3
swap 1 and 4
[4, 2, 3, 1][4, 3, 2, 1]C · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
The two-pointer swap is the reusable primitive behind reverse-string and the three-reversal rotation.
ComplexityO(n) time, O(1) space — n/2 swaps, each O(1), with only a temp variable — linear time, constant space.
E · Edge cases and pitfalls5nums = [1, 2, 3] — Middle 2 stays; result [3, 2, 1].Explore edge casesHide edge cases
Edge cases handled
nums = [1, 2, 3]Middle 2 stays; result [3, 2, 1].nums = [9]Loop never runs; unchanged.nums = []Nothing to do.
Pitfalls
- Looping the full length and swapping every pair twice (undoing the reversal).
- Off-by-one: right starting at length instead of length − 1.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic void reverse(int[] nums) { int left = 0, right = nums.length - 1; while (left < right) { int tmp = nums[left]; nums[left] = nums[right];Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
1Swapping the full length
Looping i from 0 to n−1 and swapping nums[i] with nums[n−1−i] swaps every pair twice, leaving the array unchanged.
Explore the code comparisonHide the code comparison
Swapping the full length
Looping i from 0 to n−1 and swapping nums[i] with nums[n−1−i] swaps every pair twice, leaving the array unchanged.
Don't write this
for (int i = 0; i < n; i++) swap(nums, i, n-1-i); // double swapWrite this instead
int l = 0, r = n-1; while (l < r) { swap(nums, l++, r--); }Revise
Carry the pattern into the interview
Pattern to remember
Two-pointer inward swap
Code pattern to memorize
l=0; r=n-1; while l<r: swap(l,r); l++; r--Rules to remember
- 1left=0, right=n-1.
- 2Swap, then move both in.
- 3Stop when left ≥ right.
Use this pattern when
- In-place reversal
- Symmetric two-pointer passes
Interview takeaway
Reverse-in-place = swap the ends and converge.
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: Two-pointer inward swap. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups