Reverse the Array
An easy Arrays problem included in Apna College, Love Babbar 450. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Arrays
- Sheets
- 2
- Core for
- 25 roles
- Platform
- GeeksforGeeks
The problem
Reverse the given array in place so that the first element becomes the last and vice versa.
Example 1
- Input
- [1,2,3,4,5]
- Output
- [5,4,3,2,1]
Example 2
- Input
- [7]
- Output
- [7]
Example 3
- Input
- [1,2]
- Output
- [2,1]
Constraints
- 1 <= n <= 10^5
- -10^9 <= arr[i] <= 10^9
How to think about it
Updated 2026-09-09Every element must trade places with its mirror image across the center, and each swap fixes two positions at once. Marching two indices toward each other from the boundaries until they cross finishes the job in a single sweep without touching any extra memory.
Approaches, worst first
Auxiliary array copy
time O(n) · space O(n)
Read the input from back to front into a fresh buffer, then copy every value back. Straightforward and works on any collection, but allocates memory proportional to the input size when the values could have traded places in place.
Two-pointer inward swapWrite this one
time O(n) · space O(1)
Place pointers at index 0 and n - 1. Swap their contents, advance left, decrement right, and stop when the pointers meet or cross. Performs exactly n / 2 swaps and preserves strict O(1) auxiliary space.
Where people lose marks · 3
- Iterating the loop until `i <= n` instead of `i < j` swaps everything twice and silently restores the original order.
- A single-element array must remain unchanged without out-of-bounds pointer indexing.
- Using bitwise XOR swaps on identical memory locations when left equals right in an odd-length array zeroes out the middle element.
Full solution
Two-pointer inward swap: one sweep from both ends, exactly n / 2 swaps, no extra memory. It is the in-place answer the statement asks for; the auxiliary-array copy spends O(n) space on something a swap does for free.
Python
def reverse_array(arr: list[int]) -> list[int]:
left, right = 0, len(arr) - 1
# Stop when the pointers meet or cross; looping to n would swap everything twice.
while left < right:
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1
return arr
JavaScript
function reverseArray(arr) {
let left = 0;
let right = arr.length - 1;
// Stop when the pointers meet or cross; looping to n would swap everything twice.
while (left < right) {
[arr[left], arr[right]] = [arr[right], arr[left]];
left++;
right--;
}
return arr;
}
The theory behind it
Arrays — the ground this problem stands on. All Arrays problems
What Arrays is
An array is a row of fixed boxes laid side by side in computer memory, like numbered lockers in a hallway. Because each box occupies identical space and sits directly next to its neighbors, jumping to locker zero or locker ten thousand takes the exact same tiny fraction of time. Every box holds an item of the same type, addressed by an offset number called an index.
When to reach for it
Reach for an array when items arrive in a known sequence and need immediate retrieval by position number. Problems asking for running totals, prefix accumulations, cyclic rotations, or in-place rearrangements signal array mechanics. Whenever constraints require constant-time random lookups or contiguous cache scans across fixed collections, a flat sequence is the default container.
How the pattern works
Visualize a tape with zero-indexed slots stretching from start to end. Keep track of write and read cursors when modifying contents without allocating helper buffers. For running computations, maintain an invariant such as having processed all elements left of the current index while pending elements wait to the right. When modifying entries in place, consider scanning backwards from the end so unread data is not overwritten.
What each operation costs
| Operation | Time |
|---|---|
| look up element by index | O(1) |
| insert or delete at the start | O(n) |
| search an unsorted collection for a value | O(n) |
What usually goes wrong with Arrays
- Reading past the final index by checking index less than or equal to length instead of strictly less than length, triggering index out of bounds exceptions.
- Modifying length or removing elements during a forward iteration loop, which causes remaining items to shift left and skip validation on the next neighbor.
- Assuming dynamic resizing is costless inside nested loops, causing repeated memory reallocation copies when appending unknown quantities of items.
Which roles need this problem
Arrays is a core topic for these 25 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 3 more roles, including Database Engineer, Bioinformatics Engineer, Networking Engineer.
Companies that have asked it
Tags taken from the problem's own GeeksforGeeks page — not a copied list.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Arrays problems
Problem set and role mapping as of .