Definition
- Two pointers –
leftstarts at index 0,rightstarts at the last index. - Swap and step – exchange the two elements, then
left++andright--. - Stop condition – loop while
left < right; strict less-than handles odd-length arrays cleanly. - O(n) time – n/2 swaps, each constant time.
- O(1) space – only one temporary variable for the swap; no second array allocated.
The National Bank of India has Rajesh's transaction history listed oldest first, but the mobile app must show newest first. Rather than copying into a second list, two clerks start at opposite ends of the same list, swap the two entries they are holding, then each take one step toward the middle and swap again, continuing until they meet in the centre.
Understanding the Concepts
What is the problem?
Reverse an array in place so that the first element becomes the last and the last becomes the first, without allocating a second array. In a banking context, Rajesh's mobile banking app at National Bank of India needs to flip the transaction order from oldest-first (stored) to newest-first (displayed).
How does the two-pointer swap work?
Set left = 0 and right = length - 1. While left < right, swap the elements at those positions and move both pointers inward.
package com.bank.service;
import java.math.BigDecimal;
import java.util.Arrays;
public class InPlaceReverser {
public static void reverse(BigDecimal[] arr) {
if (arr == null) {
throw new IllegalArgumentException("Array cannot be null");
}
int left = 0;
int right = arr.length - 1;
while (left < right) {
BigDecimal temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
public static void main(String[] args) {
BigDecimal[] txns = {
new BigDecimal("1000.00"),
new BigDecimal("2500.00"),
new BigDecimal("7800.00"),
new BigDecimal("400.00"),
new BigDecimal("9100.00")
};
System.out.println("Before: " + Arrays.toString(txns));
reverse(txns);
System.out.println("After: " + Arrays.toString(txns));
}
}
Before: [1000.00, 2500.00, 7800.00, 400.00, 9100.00]
After: [9100.00, 400.00, 7800.00, 2500.00, 1000.00]
Why is the strict less-than condition important?
Using left < right stops the loop exactly at the middle. For an odd-length array, the middle element does not need swapping with itself. Using left <= right would swap the middle element with itself, which is harmless but wasteful. Continuing past the middle would swap elements back, undoing the reversal.
Why is in-place reversal important?
Building a second reversed array doubles the memory usage. For a mobile app displaying a statement with thousands of transactions, that extra allocation matters. The in-place version uses only one temporary variable.
How does this compare to a reversed copy?
In-place reversal mutates the original array, which is a side effect. A reversed copy leaves the original unchanged. In production Pankaj recommends offering both methods, with clear naming to show which one mutates.
// Mutating version
public static void reverseInPlace(BigDecimal[] arr) { /* ... */ }
// Non-mutating version
public static BigDecimal[] reversedCopy(BigDecimal[] arr) {
BigDecimal[] result = new BigDecimal[arr.length];
for (int i = 0; i < arr.length; i++) {
result[i] = arr[arr.length - 1 - i];
}
return result;
}
What is the time and space complexity?
- Time: O(n) because the loop runs n/2 times.
- Space: O(1) because only one temporary variable is used.
How does array reversal relate to rotation?
Rotating an array by k positions can be done with three in-place reversals: reverse the whole array, reverse the first k elements, and reverse the remaining n-k elements. This is the elegant reversal-based rotation algorithm.
// Rotate right by k:
// [1, 2, 3, 4, 5] with k=2
// Step 1: reverse all → [5, 4, 3, 2, 1]
// Step 2: reverse [0..k-1] → [4, 5, 3, 2, 1]
// Step 3: reverse [k..n-1] → [4, 5, 1, 2, 3]
What are common mistakes?
The most common mistake is using <= instead of < in the loop condition. Another is forgetting that the method mutates its argument, leading to bugs when the caller expects the original order preserved.
What should a senior engineer know?
A senior engineer knows that in-place reversal is a fundamental subroutine used in rotation, next permutation, and palindrome algorithms. The senior also knows that for thread safety, the in-place version requires external synchronisation if the array is shared across threads. For the standard library, Collections.reverse(List) reverses a list in place, and wrapping an array as Arrays.asList(arr) only works for object arrays, not primitives.
Visual Overview
arr[]: 1000 2500 7800 400 9100
│ │
left=0 right=4
Step 1: swap(1000, 9100) → [9100, 2500, 7800, 400, 1000]
left=1 right=3
Step 2: swap(2500, 400) → [9100, 400, 7800, 2500, 1000]
left=2 right=2
Stop: left == right (7800 stays in place)
Result: [9100, 400, 7800, 2500, 1000]
Key Takeaway
Reversing an array in place uses the two-pointer technique. I put one index at the start and one at the end, and while the left index is strictly less than the right I swap the two elements, then step both inward. When they meet, the array is reversed in linear time with constant extra space.
The detail I always get right is the stopping condition: strict less-than ensures the middle element in an odd-length array stays in place without a wasted self-swap.
The API decision I highlight is mutation versus copy. In-place reversal changes the caller's array, which is efficient but a side effect. I document the mutation and provide a separate method that returns a reversed copy when the original must be preserved.
In my banking project at National Bank of India, I used this to present Rajesh's transaction references newest first on the mobile activity screen, where the stored order is oldest first.
Remember one sentence: two pointers at the ends, swap and step inward while left is strictly less than right, and document the mutation.
---