How to Reverse an Array in Java - Quiz
Total: 5 questions
1. How do you reverse an array in Java in place, without allocating extra memory?
How do you reverse an array in Java in place, without allocating extra memory?
Swap symmetric elements: iterate from the start to the middle (i < array.length / 2) and swap array[i] with array[array.length - i - 1] using a temporary variable. The loop only walks half of the array; with an odd length the center element stays in place. The algorithm runs in O(n) time and uses O(1) extra memory.
public static void invert(int[] array) {
for (int i = 0; i < array.length / 2; i++) {
int tmp = array[i];
array[i] = array[array.length - i - 1];
array[array.length - i - 1] = tmp;
}
}
2. Why does the reversal method work correctly on an empty array and a single-element array, and which cases should you test it on?
Why does the reversal method work correctly on an empty array and a single-element array, and which cases should you test it on?
Test the method on edge cases: an empty array, a single element, and both even and odd lengths. For an empty array and a single-element array, array.length / 2 equals zero, so the loop never runs and the array stays unchanged — no exception is thrown. Arrays.toString() is handy for printing:
[] => []
[0] => [0]
[0, 1] => [1, 0]
[0, 1, 2] => [2, 1, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] => [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
3. How do you reverse an array without changing the original?
How do you reverse an array without changing the original?
In-place reversal modifies the source array. To keep the original, create a new array of the same length and fill it by walking the source from the end: result[i] = source[source.length - 1 - i]. This needs O(n) extra memory but never corrupts the input.
public static int[] reversedCopy(int[] source) {
int[] result = new int[source.length];
for (int i = 0; i < source.length; i++) {
result[i] = source[source.length - 1 - i];
}
return result;
}
4. How do you reverse an int[] array with a Stream?
How do you reverse an int[] array with a Stream?
For an int[] you can reverse elements declaratively with IntStream, iterating indices from the end. It reads more compactly, but for primitives it is slower than a plain loop and always allocates a new array. In interviews, the manual in-place reversal is usually what is expected.
int[] source = {1, 2, 3, 4, 5};
int[] reversed = IntStream.rangeClosed(1, source.length)
.map(i -> source[source.length - i])
.toArray();
System.out.println(Arrays.toString(reversed)); // [5, 4, 3, 2, 1]
5. How do you reverse a List and an array of objects, and does Java have a built-in method to reverse an array?
How do you reverse a List and an array of objects, and does Java have a built-in method to reverse an array?
A List has a built-in Collections.reverse() that reverses it in place. For a primitive array there is no built-in Arrays.reverse() in the standard library — you write the loop by hand or use ArrayUtils.reverse() from Apache Commons Lang. An array of objects is reversed with the same swap algorithm through a generic method (generics don't work with primitives, so int[] needs a separate version). Common mistakes: looping to the end instead of the middle, calling Collections.reverse() on an array, and Arrays.asList(intArray), which returns a list with a single element — the array itself.
public static <T> void invert(T[] array) {
for (int i = 0; i < array.length / 2; i++) {
T tmp = array[i];
array[i] = array[array.length - i - 1];
array[array.length - i - 1] = tmp;
}
}