Is it possible to invert an array with constant extra space?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Yes, reversing an array with constant extra space is absolutely possible. The standard solution is an in-place two-pointer swap, which uses only a few variables regardless of array size and therefore has O(1) auxiliary space complexity.
The In-Place Idea
To reverse an array, you do not need a second array. You only need to swap matching elements from the ends toward the middle:
- first with last
- second with second-last
- third with third-last
Continue until the pointers meet or cross.
For an array of length n, element i swaps with element n - 1 - i.
A Simple Python Implementation
Output:
The extra space stays constant because only left, right, and the temporary swap state are needed.
The Same Idea in C++
This also runs in O(n) time because each element is moved at most once.
Why the Space Is Constant
Constant extra space does not mean “no extra bytes at all.” It means the amount of additional memory does not grow with input size.
This algorithm uses a fixed number of variables:
- one pointer or index for the left side
- one pointer or index for the right side
- one temporary location during a swap, depending on the language
Whether the array has 5 elements or 5 million, the algorithm still needs only that small, fixed amount of extra storage.
Odd and Even Length Arrays
The algorithm works for both:
- Even length: every element participates in one swap.
- Odd length: the middle element stays where it is.
For example, [1, 2, 3, 4, 5] becomes [5, 4, 3, 2, 1], and the value 3 never needs to move because it is already in the correct middle position after the surrounding swaps.
When You Cannot Do It In Place
The answer changes only if the data structure is immutable. For example, some languages use immutable array-like structures or strings, which means any “reverse” operation must create a new value. In that case, the algorithmic idea is still valid, but the specific data type prevents in-place mutation.
For a normal mutable array, though, constant-space reversal is the standard solution.
Common Pitfalls
The most common mistake is accidentally allocating a second array and calling the result “constant space.” If the extra structure grows with n, the algorithm is no longer O(1) space.
Another issue is off-by-one errors in the right pointer. The last valid index is len(values) - 1, not len(values).
Developers also sometimes continue the loop while left <= right. That still works in some languages, but it causes an unnecessary self-swap of the middle element for odd lengths.
Finally, be careful with unsigned indexes in languages such as C++. If the array is empty, subtracting from zero without a guard can underflow.
Summary
- Yes, arrays can be reversed with constant extra space.
- The standard method swaps from both ends toward the center.
- The algorithm uses
O(n)time andO(1)extra space. - It works naturally for both odd and even lengths.
- Watch out for off-by-one errors and empty-array edge cases.

