Title: Check if an Array is Symmetric
In this guide, you’ll learn how to check if an array is symmetric in Java. We’ll walk through the approach, a complete working program, a step-by-step trace, and the edge cases and follow-up questions interviewers like to ask about this problem.
Understanding the Problem
An array of size 2n is called symmetric when its first half mirrors its second half element by element. This is different from a palindrome check, where the whole array reads the same forwards and backwards. For a symmetric array, every index i from 0 to n - 1 must satisfy arr[i] == arr[n + i].
This problem is a common interview warm-up because it looks like a palindrome check at first glance, but the comparison window is different. Getting the index math right, especially the midpoint, is the real skill being tested here.
Symmetric arrays show up when validating paired data, such as two halves of a buffer that should mirror each other, or when confirming that a split operation preserved structure on both sides.
Approach to Check if an Array is Symmetric
- Read the array
arrand compute its lengthlen. - If
lenis odd, the array cannot be split into two equal halves, so returnfalseimmediately. - Compute the midpoint
half = len / 2. - Loop
ifrom0tohalf - 1. - At each step, compare
arr[i]witharr[half + i]. - If any pair differs, return
falseright away; there is no need to check the rest. - If the loop finishes without a mismatch, the array is symmetric, so return
true.
Java Program to Check if an Array is Symmetric
public class SymmetricArrayChecker {
public static boolean isSymmetric(int[] arr) {
int len = arr.length;
if (len % 2 != 0) {
return false;
}
int half = len / 2;
for (int i = 0; i < half; i++) {
if (arr[i] != arr[half + i]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 1, 2, 3};
int[] arr2 = {1, 2, 3, 4, 5, 6};
System.out.println("arr1 is symmetric: " + isSymmetric(arr1));
System.out.println("arr2 is symmetric: " + isSymmetric(arr2));
}
}Explanation of the Program
The method isSymmetric starts by checking whether len is even. An odd-length array has no exact midpoint, so it fails the symmetry test before any comparison happens.
Once the length check passes, half marks the boundary between the two halves. The loop variable i walks through the first half while half + i walks through the matching position in the second half.
As soon as arr[i] != arr[half + i], the method returns false and skips the remaining comparisons. This short-circuit avoids wasted work once a mismatch is found.
If the loop finishes without returning early, every pair matched, so the method returns true.
Step-by-Step Trace
For the array {4, 7, 2, 4, 7, 9} (length 6, so half = 3), the trace below shows what i compares and whether the result is a match.
| Iteration | i | arr[i] | arr[half + i] | Match? |
|---|---|---|---|---|
| 1 | 0 | 4 | 4 | yes |
| 2 | 1 | 7 | 7 | yes |
| 3 | 2 | 2 | 9 | no, returns false |
At the start of iteration i, every earlier pair from 0 through i - 1 has already matched. The loop exits the moment one pair fails, which is exactly what happens at i = 2 above.
Examples
Example 1: Symmetric Array
int[] arr = {10, 20, 30, 10, 20, 30};
System.out.println(isSymmetric(arr));
// trueHere the first half {10, 20, 30} matches the second half {10, 20, 30} at every position, so the array is symmetric.
Example 2: Non-Symmetric Array
int[] arr = {5, 15, 25, 35, 45, 55};
System.out.println(isSymmetric(arr));
// falseThe first element of the first half, 5, does not equal the first element of the second half, 35, so the check fails on the very first comparison.
Example 3: Odd-Length Array
int[] arr = {1, 2, 3, 4, 5};
System.out.println(isSymmetric(arr));
// false, odd lengthAn array with an odd length has no element that splits it into two equal halves, so isSymmetric returns false before the loop even starts.
Common Mistakes to Avoid
These are the mistakes that most often turn a working symmetric check into a buggy one.
- Confusing symmetric with palindrome – comparing
arr[i]witharr[len - 1 - i]checks if the array reads the same backwards, not whether the two halves mirror each other. Comparearr[i]witharr[half + i]instead. - Forgetting the odd-length check – running the loop on an odd-length array lets integer division silently drop the middle element and can produce a wrong answer. Return
falseimmediately whenlen % 2 != 0. - Looping to len instead of half – iterating all the way to
lenre-compares every pair twice and throwsArrayIndexOutOfBoundsExceptiononceipasseshalf. Stop the loop athalf - 1. - Not short-circuiting on mismatch – continuing to loop after finding a mismatch wastes comparisons on an array that is already known not to be symmetric. Return
falseas soon as one pair differs.
Edge Cases to Test
A thorough answer walks through these input shapes before being asked.
- Empty array
- Odd-length array
- Single pair (length 2)
- All elements identical
- Array with negative numbers
- Array with duplicate values across both halves
- Integer.MIN_VALUE / Integer.MAX_VALUE present
- Null array reference
Additional Considerations
The algorithm runs in O(n) time because it visits at most half of the array’s elements, each compared exactly once before an early return or a successful finish.
Space complexity is O(1) since the check only needs the half variable and the loop index; no auxiliary array or data structure is allocated.
Passing a null array causes isSymmetric to throw a NullPointerException at arr.length, so callers working with untrusted input should check for null before calling this method.
An empty array has len = 0, which is even, and half = 0 means the loop body never runs. The method returns true for an empty array, which is the mathematically consistent result since there are no pairs to disagree.
Interview Follow-up Questions
Once the base solution is working, interviewers often push further with questions like these.
- What if the array must remain unmodified and read-only? – the current approach already only reads each element once with two index variables, so no adaptation is needed.
- What if you need to check symmetry for an array of objects instead of primitives? – replace the
!=comparison with.equals(), and handle null elements explicitly to avoid aNullPointerException. - What if the array is extremely large and streamed in chunks? – you cannot know
halfuntil the full length is known, so buffer the first half while reading, then compare it against the second half as it arrives. - What if you need to find the longest symmetric subarray instead of checking the whole array? – expand the two-half comparison around every possible midpoint, which raises the overall complexity to O(n^2) unless you add further optimization such as hashing partial sums.
Conclusion
Checking if an array is symmetric comes down to comparing two halves at matching offsets, and handling the odd-length case up front keeps the logic simple and correct.
Try adapting isSymmetric to work with other data types, or to report the first mismatching index instead of just a boolean. It is a good way to practice the same index arithmetic in a slightly different context. You can learn more about array-based problems like this one in our Arrays category.



