Rotate Array Java | Fresher Interview
Fast recall
Quick revision
Core answer
Whether you know the in-place reversal algorithm and the k %= n normalization
Forgetting k %= n (k > n loops), or using O(n) extra space when O(1) is possible
Derive the three-reversal trick and handle k > n with the modulo up front
Interview-ready guide
Interview answer
To rotate an array to the right by k, the slick in-place approach uses three reversals.
But before anything, I normalize k by taking k modulo n, because rotating by the array's length is a no-op, and k might be larger than the array. Then the algorithm is: reverse the entire array, then reverse just the first k elements, then reverse the remaining n minus k elements.
After those three steps, the array is rotated right by k, and it's done in O of n time and O of one space, completely in place. The intuition is that reversing the whole array brings the last k elements to the front, but they end up in reverse order, and reversing each of the two pieces afterward restores their correct internal order. Let me give a quick example.
With one through five and k equals two, reversing the whole thing gives five, four, three, two, one. Reversing the first two gives four, five, three, two, one. Then reversing the last three gives four, five, one, two, three, which is exactly the original rotated right by two.
The mistakes to avoid are forgetting the k modulo n step, which breaks when k is greater than or equal to n, and getting the segment boundaries wrong, the first segment is index zero to k minus one and the second is k to n minus one.
There are other approaches, copying into a new array is O of n space, and shifting one position at a time k times is O of n times k, which is far too slow, so the reversal trick is the one I reach for. To rotate left by k instead, I just rotate right by n minus k.
Rotate Array Java | Fresher Interview
Direct Answer
Rotate right by k using the reversal trick in O(n) time, O(1) space: first k %= n (k can exceed n), then reverse the whole array, reverse the first k, and reverse the rest. The three reversals leave the array rotated right by k. The naive 'shift one position k times' is O(n*k); an extra-array copy is O(n) time but O(n) space.
Deep Dive & Architecture
Think of the array as two parts: A contains the first n - k elements and B contains the last k. A right rotation changes A B into B A.
Reversing the complete array produces reverse(B) reverse(A). Reversing those two parts separately restores the order inside each part, leaving exactly B A.
Worked example
For [1, 2, 3, 4, 5] and k = 2, A = [1, 2, 3] and B = [4, 5]. Reverse everything to get [5, 4, 3, 2, 1]. Reverse indexes 0 through 1 to get [4, 5, 3, 2, 1]. Reverse indexes 2 through 4 to get [4, 5, 1, 2, 3], which is the required right rotation.
Normalize k with k %= n so rotating by 7 positions in a five-element array behaves like rotating by 2. Handle an empty array before that calculation to avoid division by zero. If negative rotations are allowed, normalize with ((k % n) + n) % n.
Every element participates in a constant number of swaps, so the method is O(n) time and O(1) extra space.
Code Example
void rotate(int[] a, int k) {
int n = a.length;
k %= n; // handle k > n
reverse(a, 0, n - 1); // reverse all
reverse(a, 0, k - 1); // reverse first k
reverse(a, k, n - 1); // reverse the rest
}
void reverse(int[] a, int i, int j) {
while (i < j) { int t = a[i]; a[i++] = a[j]; a[j--] = t; }
}
// a = {1,2,3,4,5}, k = 2
// reverse all -> {5,4,3,2,1}
// reverse first 2 -> {4,5,3,2,1}
// reverse rest -> {4,5,1,2,3} (rotated right by 2)
Common Mistakes & Pitfalls
- Forgetting
k %= n: whenk > n(ork == n), indices break or you do useless work. - Wrong segment bounds: first segment is
[0, k-1], second is[k, n-1]. - O(n) extra space: the reversal method is O(1); don't copy unless asked.
- O(n*k) shifting: rotating one position at a time is far too slow for large k.
Check your understanding
Answer these without looking back at the article.
- How do you write a FizzBuzz program in Java?
- How do you check if a number is prime in Java?
- How do you find the second largest element in an array?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.