Reverse an Array
Table of Contents + −
This is one of the first array questions you will ever get in an interview. It looks easy. But the interviewer is not really asking you to reverse an array. They want to see if you can do it in place, without making a second array.
🔄 The Problem
Given an array of numbers, reverse it. The first element should become the last, the last should become the first, and so on.
Here is what that looks like:
Input: [1, 2, 3, 4, 5]Output: [5, 4, 3, 2, 1]The catch is the follow-up the interviewer almost always adds: do it in place. In place means you change the same array directly. You do not create a new array to hold the answer. So you are only allowed a tiny bit of extra memory, like one or two variables.
🧠 The Simple Idea First
Before the smart way, let’s think about the obvious way. You could make a new empty array. Then you walk the original from the last element to the first, and push each one into the new array.
That works. But it uses a whole second array. So the extra memory grows with the input. That is O(n) extra space, and the interviewer wanted O(1). So this is the version to mention, then improve.
✌️ The Two Pointer Way
Here is the trick interviewers want. It is called the two pointer technique. You keep two markers:
- One pointer called
leftstarts at the very first index. - One pointer called
rightstarts at the very last index.
Then you just swap the two values they point at, move left one step right, move right one step left, and repeat. You stop when they meet in the middle.
Think of it like two people standing at opposite ends of a line of boxes. They each grab a box, trade boxes, then step toward each other. By the time they meet, the whole line is reversed.
Steps to reverse an array in place
Let’s write the steps down before any code.
- Set
left = 0andright = n - 1, wherenis the number of elements. - While
left < right:- Swap
array[left]andarray[right]. - Do
left = left + 1. - Do
right = right - 1.
- Swap
- When
leftandrightcross, the array is fully reversed.
Why we stop in the middle
Each swap fixes two elements at once, the one on the left and the one on the right. So we only need to walk halfway. If you keep going past the middle, you swap everything back and undo your own work.
Code to reverse an array in place
Now let’s see it in every language.
# function to reverse an array in placedef reverse_array(arr): left = 0 right = len(arr) - 1
# swap until the two pointers meet while left < right: arr[left], arr[right] = arr[right], arr[left] left += 1 right -= 1
def main(): arr = [1, 2, 3, 4, 5] reverse_array(arr) print("Reversed array:", arr)
if __name__ == "__main__": main()The output of the above code will be:
Reversed array: [5, 4, 3, 2, 1]⏱️ Time and Space Complexity
| Approach | Time | Space |
|---|---|---|
| New array (brute force) | O(n) | O(n) |
| Two pointer (in place) | O(n) | O(1) |
Both ways look at each element once, so both are O(n) time. The win is in space. The two pointer way only uses a couple of variables, so it is O(1) extra space. That is the answer the interviewer is hoping for.
Built-in reverse functions
Most languages have a built-in like Python’s arr.reverse() or Java’s Collections.reverse(). In a real project, use those. But in an interview, write the two pointer version yourself, because they want to see that you understand how it works.
🧩 Key Takeaways
- ✅ “Reverse an array” almost always means reverse it in place, with O(1) extra space.
- ✅ The two pointer technique swaps the ends and walks inward until the pointers meet.
- ✅ You only loop halfway, because each swap fixes two elements at once.
- ✅ Time is O(n) and extra space is O(1).
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does it mean to reverse an array 'in place'?
Why: In place means you modify the original array directly, using only O(1) extra memory like a couple of variables.
- 2
In the two pointer method, when do you stop swapping?
Why: Each swap fixes two elements, so you only walk halfway. You stop when left and right cross in the middle.
- 3
What is the extra space complexity of the two pointer approach?
Why: It only uses a few variables (left, right, temp), so the extra space does not grow with the input. That is O(1).
- 4
Why is the 'make a new array' approach weaker in an interview?
Why: It works and is still O(n) time, but it needs a second array, so it uses O(n) extra space. The interviewer wants O(1).
🚀 What’s Next?
Now that you have the two pointer trick, try these next:
- Rotate an Array uses array reversal as a clever building block.
- Check Palindrome String uses the same two pointer idea on a string.
Reverse an array shows up everywhere once you learn it. Get the two pointer pattern into your fingers and a whole family of problems gets easier.