Interviewers like this because it checks whether you can partition data cleanly instead of overcomplicating a tiny problem.
Question: Group even and odd numbers.
Answer: Put all even numbers together and all odd numbers together, usually in one array or list. The simplest fast solution is a two-pointer partition: scan from both ends, swap a wrong odd on the left with a wrong even on the right, and keep going until the pointers meet. This gives O(n) time and O(1) extra space. If the order inside each group matters, use extra storage for a stable version.
Interview-Ready Answer: I’d solve this with a two-pointer partition. I keep one pointer at the left and one at the right; when the left side stops on an odd number and the right side stops on an even number, I swap them. That groups all evens on one side and odds on the other in linear time, O(n), with constant extra space. If the interviewer asks to preserve the original order, I’d switch to a stable version using an extra list.
In interview language, group usually means partition: split values into two buckets, evens and odds. Unlike sorting, you do not need 2 before 4 before 6; you only need each number on the correct side.
This works because every swap fixes two mistakes at once: an odd number that was sitting too far left and an even number that was sitting too far right.
Parity means whether a number is even or odd. In Java, n % 2 == 0 means even, and n % 2 != 0 means odd. A common alternative is (n & 1) == 0; the bitwise & operator looks at the last binary bit, which is 0 for even and 1 for odd. Both work for negative numbers too, but % 2 != 0 is easier for beginners to read.
| Method | Order | Space | Best for |
|---|---|---|---|
| Two-pointer | Unstable | O(1) | Low memory |
| Extra lists | Stable | O(n) | Keep order |
The time complexity is O(n) because each pointer only moves forward or backward, and each element is examined a small number of times. In practice, if you had 1,000,000 integers, the algorithm still stays linear; it just does one pass over the data instead of repeatedly re-scanning it like a nested-loop solution would.
Memory model: the algorithm stores only a few indexes and one temporary variable for swapping, so the extra space is constant.
Real-World Story: Imagine a checkout service that batches order IDs before sending them to two worker pools, one for even IDs and one for odd IDs, so the system can split load evenly. The grouping step is tiny, but it protects the next stage from doing unnecessary checks and keeps the pipeline predictable.
What goes wrong if a developer assumes the grouping is stable when it is not? The same IDs still appear, but their order changes after swapping, so a reconciliation job comparing the grouped output against an ordered audit feed starts reporting mismatches. Symptoms include confusing log lines like sequence mismatch or unexpected order, operators chasing a phantom data corruption bug, and reports that look wrong even though the raw numbers are correct.
import java.util.Arrays;\n\npublic class GroupEvenOdd {\n public static void main(String[] args) {\n int[][] samples = {\n {5, 2, 7, 8, 1, 4, 6, 3},\n {2, 4, 6, 8},\n {1, 3, 5},\n {0, -1, -2, -3, 4},\n {},\n null\n };\n\n for (int[] sample : samples) {\n System.out.println("Before: " + toPrintable(sample));\n groupEvenOddInPlace(sample);\n System.out.println("After : " + toPrintable(sample));\n System.out.println();\n }\n }\n\n public static void groupEvenOddInPlace(int[] arr) {\n if (arr == null) {\n System.out.println("Cannot group a null array.");\n return;\n }\n\n if (arr.length <= 1) {\n // Nothing to fix: a zero- or one-element array is already grouped.\n return;\n }\n\n int left = 0;\n int right = arr.length - 1;\n\n while (left < right) {\n // Keep moving left while the value is already on the correct side.\n while (left < right && isEven(arr[left])) {\n left++;\n }\n\n // Keep moving right while the value is already on the correct side.\n while (left < right && !isEven(arr[right])) {\n right--;\n }\n\n // Now left points to an odd number and right points to an even number.\n // Swapping fixes two misplaced elements in one shot.\n if (left < right) {\n swap(arr, left, right);\n left++;\n right--;\n }\n }\n }\n\n private static boolean isEven(int n) {\n // Modulo is easy to read and works for negative numbers too.\n return n % 2 == 0;\n }\n\n private static void swap(int[] arr, int i, int j) {\n int temp = arr[i];\n arr[i] = arr[j];\n arr[j] = temp;\n }\n\n private static String toPrintable(int[] arr) {\n return arr == null ? "null" : Arrays.toString(arr);\n }\n}\n& 1 instead of % 2? Both are correct for parity, but & 1 is a fast bitwise check that directly reads the last binary digit. In interviews, readability usually matters more, so % 2 is perfectly fine.n % 2 == 1 a correct odd test in Java? No. For negative odd numbers, Java gives a negative remainder, so -3 % 2 is -1, not 1. Use n % 2 != 0 or (n & 1) != 0 instead.Common Mistakes:
% 2 == 1 for odd numbers. Correction: use % 2 != 0 or & 1 so negative odd numbers are handled correctly.0 as part of the even group.Memory Hook: Think of two bouncers in a hallway: the left bouncer pushes evens to the left door, the right bouncer pushes odds to the right door, and any misplaced guest gets swapped out immediately.
Cheat Sheet:
Group usually means partition, not sort.Practice Tasks: