Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Return the array in reverse order. The clean in-place answer is two pointers: one at the start, one at the end, swap them and move inward until they meet. That's O(n) time and O(1) extra space — better than building a new reversed array, which costs O(n) space.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you reach for an in-place two-pointer swap rather than allocating a new array, and whether you get the loop boundary (left < right) and swap right.
Where candidates slip
Looping all the way to n−1 and swapping each element twice — which reverses the array and then reverses it back to the original. Or using left <= right and pointlessly swapping the middle element with itself.
How to stand out
State the O(1)-space in-place version up front, note it's exactly n/2 swaps, and mention the language built-ins exist but you're implementing the mechanic the question is testing.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Interactive decision treeInteractiveChoose the right approachFirst decision previewleft = 0, right = n-1left < right ?YesNoExplore the full treeCollapse the tree
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 2
Baseline — Build a New Reversed Array
1Hint 1Explore hintHide hint
The simplest idea: allocate a fresh array and copy elements from the end of the source to the front of the destination.
2Hint 2Explore hintHide hint
Where does a[i] land in the new array? At index n−1−i.
3Hint 3Explore hintHide hint
This is correct and O(n) time — but how much extra memory does it use, and can you avoid it?
A · Core idea
Allocate a new array of the same length and fill it by reading the source from the back.
Why it's the natural first idea. 'Reverse' reads as 'produce a new sequence in opposite order', so copying source index i to destination index n−1−i is a direct translation. It's easy to get right and easy to reason about.
Why we usually don't ship it. It allocates a whole second array — O(n) extra memory — to do something that can be done by rearranging the original. When the prompt allows mutation, that allocation is pure waste. The only time you'd keep it is when the caller must retain the original array unchanged.
The pivot to in-place. Reversing only ever swaps the element at i with the one at n−1−i. Each element has exactly one partner, and once both have been swapped you never touch them again. So you can do the whole thing by swapping pairs in the original array — no second buffer.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
a = [1, 4, 3, 2]Array state
fill out[0]
out[0] = a[3]
[2, 3, 4, 1]C · Code with teaching comments
Reference implementation previewpublic int[] reverseArray(int[] a) { int n = a.length; int[] out = new int[n]; for (int i = 0; i < n; i++) { out[i] = a[n - 1 - i];Explore the full codeHide the full code
D · Why it works and complexityO(n) timeO(n) spaceOpen the invariant and proofExplore the proofHide the proof
Reversal is a set of independent pairwise swaps (i with n−1−i). Recognizing that each element has a single mirror partner is what lets you drop the extra array and swap in place.
ComplexityO(n) time, O(n) space — One pass to copy n elements is O(n) time, but the second array costs O(n) extra space.
E · Edge cases and pitfalls5single element — out[0] = a[0]; same array.Explore edge casesHide edge cases
Edge cases handled
single elementout[0] = a[0]; same array.empty arrayLoop doesn't run; returns an empty array.
Pitfalls
- Using O(n) extra space when in-place is allowed.
- Off-by-one on the mirror index (n−1−i, not n−i).
- Mutating the source while also reading it if you tried to reverse 'in place' with this copy pattern.
Approach 2 of 2 · Optimal
Two Pointers — In-Place Swap (Optimal)
1Hint 1Explore hintHide hint
Place one pointer at the start and one at the end. What single operation makes both ends correct at once?
2Hint 2Explore hintHide hint
Swap a[left] and a[right], then move left forward and right backward.
3Hint 3Explore hintHide hint
When should the loop stop? Think about what happens when the pointers meet or cross.
A · Core idea
Use two indices, left starting at 0 and right at n−1, swap the elements they point to, then step both toward the center.
Why swapping fixes two cells per step. A reversal sends index i to n−1−i. Swapping a[left] with a[right] places both of those elements in their final positions simultaneously, so you only need n/2 iterations instead of n moves.
Why the loop condition is `left < right`. When left == right you're at the exact middle of an odd-length array — that single element is already where it belongs, so swapping it with itself is a no-op you should skip. Using <= does that pointless self-swap; using < stops cleanly. When left > right the two halves have fully crossed and every pair is done.
Why it's O(1) space. Everything happens inside the original array; the only extra storage is the two index variables (and a temp in Java, or none in Python's tuple swap). Compared to the new-array approach, you've traded O(n) memory for nothing — same time, less space.
Why this generalizes. The converging-pointers mechanic is the backbone of palindrome checks (compare instead of swap) and the reverse-three-segments trick for array rotation. Mastering it here pays off broadly.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
a = [1, 4, 3, 2]Array state
swap ends
a[0] ↔ a[3]
[2, 3, 4, 1]C · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
One swap settles two elements, so reversal needs only n/2 operations. The left < right stop condition is the small detail that separates a correct reversal from one that either self-swaps the middle or, worse, reverses twice back to the start.
ComplexityO(n) time, O(1) space — The loop runs n/2 times, each doing a constant-cost swap → O(n) time. No auxiliary array → O(1) extra space.
E · Edge cases and pitfalls5odd length — Middle element is left untouched because left < right becomes false there.Explore edge casesHide edge cases
Edge cases handled
odd lengthMiddle element is left untouched because left < right becomes false there.single elementleft < right is false immediately; no swap.
Pitfalls
- Using left <= right and self-swapping the middle element.
- Looping left over the whole array and swapping a[i] with a[n−1−i] for all i — reverses, then reverses back to the original.
- Forgetting the temp in Java and overwriting one value before reading it.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int[] reverseArray(int[] a) { int left = 0, right = a.length - 1; while (left < right) { int tmp = a[left]; a[left] = a[right];Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
1Double reversal
Looping i over all n indices and swapping a[i] with a[n−1−i] swaps every pair twice — the array ends up unchanged.
Explore the code comparisonHide the code comparison
Double reversal
Looping i over all n indices and swapping a[i] with a[n−1−i] swaps every pair twice — the array ends up unchanged.
Don't write this
for (int i = 0; i < a.length; i++) {
int tmp = a[i];
a[i] = a[a.length - 1 - i];
a[a.length - 1 - i] = tmp;
}Write this instead
int left = 0, right = a.length - 1;
while (left < right) {
int tmp = a[left];
a[left++] = a[right];
a[right--] = tmp;
}2Overwriting without a temp (Java)
Assigning a[left] = a[right] before saving a[left] loses the left value, so the swap corrupts the array.
Explore the code comparisonHide the code comparison
Overwriting without a temp (Java)
Assigning a[left] = a[right] before saving a[left] loses the left value, so the swap corrupts the array.
Don't write this
a[left] = a[right];
a[right] = a[left];Write this instead
int tmp = a[left];
a[left] = a[right];
a[right] = tmp;3Self-swapping the middle
With left <= right, when the pointers meet on the center element you swap it with itself — harmless but signals an off boundary.
Explore the code comparisonHide the code comparison
Self-swapping the middle
With left <= right, when the pointers meet on the center element you swap it with itself — harmless but signals an off boundary.
Don't write this
while left <= right:
a[left], a[right] = a[right], a[left]
left += 1
right -= 1Write this instead
while left < right:
a[left], a[right] = a[right], a[left]
left += 1
right -= 1Revise
Carry the pattern into the interview
Pattern to remember
Two pointers swapping inward
Code pattern to memorize
left = 0; right = n - 1
while left < right:
swap(a[left], a[right])
left += 1; right -= 1Rules to remember
- 1Loop while
left < right— not≤. When they're equal it's the middle element, which stays put. - 2Swap in place to keep extra space at O(1); building a new array is O(n) space and usually unnecessary.
- 3You do exactly
n/2swaps — each pair is touched once. - 4In Python you can write
a[left], a[right] = a[right], a[left]for an atomic swap with no temp variable. - 5Don't confuse 'reverse in place' with
reversed()/Collections.reverseif the prompt asks you to implement it.
Use this pattern when
- Reverse an array or string in place with no extra buffer
- Any 'mirror the ends inward' operation (palindrome check, rotate-by-reversal)
- You need O(1) extra space and random access is available
Choose another pattern when
- Data is a singly linked list → pointer relinking, not index swapping
- You must keep the original and produce a separate reversed copy → allocate a new array
- Reversal is only part of a rotation → reverse three segments (rotate array)
Interview takeaway
Reverse-in-place is the canonical two-pointer warm-up: start and end pointers swap and march toward each other, stopping when they meet. O(n) time, O(1) space, n/2 swaps. The same converging-pointers idea reappears in palindrome checks and array rotation.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternConverging two pointers. Start and end indices swap and march toward the center, stopping at left < right. O(n) time, O(1) space, n/2 swaps. The mechanic underpins palindrome checks and the reverse-three-segments array rotation.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Reverse Words in a StringComing soonReverse the whole string, then reverse each word — two applications of the same swap.
- Rotate ArrayRotate by reversing three segments: whole array, first k, then the rest.
- Valid PalindromeComing soonSame converging pointers, but compare instead of swap.