It’s relatively easy to reverse an array if you have the luxury to use another array, but how would you reverse an array if a temporary buffer is not allowed? This is one of the testing array interview questions, which often proved tricky for Java and other beginner Programmers. But, don’t worry, I’ll tell you how you can solve this problem without losing your cool. Well, you can also *reverse an array in place *without using an additional buffer. If you know how to access array elements and how to loop over an array in Java using traditional for loop, you can easily solve this problem without using additional space or in-place as described in many Algorithms books and courses, and on Coding interviews.

All you need to do is a loop over the array from start to the middle element and swap the first element to the last, second element to the second last until you reach the middle element. Once you reach the middle element, your array is already sorted, and that too without using any additional space or in-place as asked in this question.

You can even use this algorithm to reverse a String in Java as well. After all, a String is backed by a character array in Java and other programming languages like C and C++. This is as simple as it could be, but you know, this is also the *fastest way to reverse an array in Java*.

In general, Data structure and Algorithm questions like ones based upon the array, linked list, binary tree, hash table, and searching/sorting algorithms are very important for programming job interviews and you should have a good knowledge of them.

## How to Reverse an array in-place in Java

In the last section, I have explained to you the logic or algorithm to reverse an array in place, now is the time to convert that algorithm into pseudo-code and then real code in Java. You will also calculate the time and space complexity of our solution, which is often required in Interviews as well as in the real world to meet your performance SLA.

So, let’s see an example of how you can reverse an array of String in Java in place.

This program doesn’t use a temporary buffer or another array, instead, it just loops over the array and swaps elements like starting from the first element, it swaps the first to last, second to the second last, until it reaches the middle of the array.

At this point all elements are already swapped, so your array is fully reversed.

This is a simple algorithm and time complexity is** O(n/2)** or **O(n)** because it needs to iterate over almost half the elements and perform n/2 swapping as well. The space complexity of the algorithm is **O(1)** because no matter how big the array is, it requires the same space.

Obviously, all-in-place algorithms have constant space complexity. Btw, if you have trouble understanding Big O notation and how to calculate the time and space complexity of any arbitrary algorithm then I suggest you check out **Algorithms and Data Structures – Part 1 and 2 **courses on Pluralsight, which will teach you the technique of calculating these numbers.

## Java Program to Reverse an array of String in place

Here is our Java program which implements this algorithm to sort a string array. You can use this algorithm to sort any kind of array like a boolean array, int array, String array, or any custom object array.

This is also the fastest way to reverse an array in Java. It cannot be faster than this because we are only accessing an array which is a constant time operation. The only thing you can optimize is to minimize swapping. Do you know any other way to make this algorithm faster? If yes, then please let us know.

If you want to solve this problem by using recursion instead of the iterative way (using for loop), you can customize your algorithm like below:

package coding; import java.util.Arrays; /** * Java Program to reverse array in place. Time complexity is O(n) *You cannot use additional buffer but one or two variables are fine. * * @author WINDOWS 8 * */ public class ReverseArrayInPlace { public static void main(String args[]){ String[] names = {"John", "Jammy", "Luke"}; System.out.println("original array: " + Arrays.toString(names) ); // reversing array with three elements reverse(names); System.out.println("reversed array: " + Arrays.toString(names) ); String[] test = {"John"}; System.out.println("original array: " + Arrays.toString(test) ); // testing reverse array function with array of just one element reverse(test); System.out.println("reversed array: " + Arrays.toString(test) ); } /** * Java Method to reverse String array in place * * @param array */ public static void reverse(String[] array) { if (array == null || array.length < 2) { return; } for (int i = 0; i < array.length / 2; i++) { String temp = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = temp; } } } Output : original array: [John, Jammy, Luke] reversed array: [Luke, Jammy, John] original array: [John] reversed array: [John]

You can see from the output that our program is working fine for an odd number of elements. I haven’t tested it for all kinds of input like reversing an array of the even number of elements, but I expect it to work.

That’s all about **how to reverse an array in place in Java**. This is one of the essential coding exercises for Java programmers learning data structure and algorithms. Remember, it’s an in-place algorithm hence, the space complexity is O(1) and the time complexity of this solution is O(n). This is also the fastest way to reverse the array in Java.

Pingback: JAVA TOP 50 PROGRAMMING QUESTIONS WITH SOLUTIONS - GRAD JOB OPENINGS