Most beginners try to memorize DSA problems.
But interviews don’t test memory — they test patterns.
If you master just these 3 array problems, you’ll unlock a huge part of DSA.
1. Reverse an Array
Idea:
Swap elements from both ends until you reach the middle.
public static void reverse(int[] arr) {
int left = 0, right = arr.length - 1;
while (left < right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++; right--;
}
}
Complexity:
- Time → O(n)
- Space → O(1)
2. Rotate Array by k Positions
Idea:
Instead of shifting one-by-one, use 3 reversals.
public static void rotate(int[] arr, int k) {
k = k % arr.length;
reverse(arr, 0, arr.length - 1);
reverse(arr, 0, k - 1);
reverse(arr, k, arr.length - 1);
}
private static void reverse(int[] arr, int start, int end) {
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++; end--;
}
}
Complexity:
- Time → O(n)
- Space → O(1)
3. Merge Two Sorted Arrays
Idea:
Use two pointers and always pick the smaller element.
public static int[] merge(int[] a, int[] b) {
int[] result = new int[a.length + b.length];
int i = 0, j = 0, k = 0;
while (i < a.length && j < b.length) {
if (a[i] < b[j]) result[k++] = a[i++];
else result[k++] = b[j++];
}
while (i < a.length) result[k++] = a[i++];
while (j < b.length) result[k++] = b[j++];
return result;
}
Complexity:
- Time → O(n + m)
- Space → O(n + m)
The Real Insight
These problems are not random.
They teach you:
- Two-pointer technique
- In-place modification
- Efficient traversal
- Pattern thinking
Key Insights
- Reverse → swapping technique
- Rotate → smart use of reverse
- Merge → two pointers
These patterns repeat in advanced problems
For More: https://www.quipoin.com/tutorial/data-structure-with-java/array-problems