Scrivio Interview library
CodingFoundations7 min read · updated 12 November 2026

Write a Java method that rotates an int array to the right by k steps in place ([1,2,3,4,5,6,7] with k = 3 gives [5,6,7,1,2,3,4]). Use O(1) extra space and handle k larger than the array length.

First we reduce k with k = k % n, because rotating by the array length changes nothing, and we return early when the array is empty. Then we reverse the whole array, reverse the first k elements, and reverse the remaining n - k elements; each reverse is a two-pointer swap, so the method runs in O(n) time with O(1) extra space. Copying into a new array is simpler but costs O(n) space, and cyclic replacement is another O(1) approach whose loop must be written carefully when n and k share a divisor.

Why this question is asked

Rotating an array looks like a toy problem, but it appears in real code whenever we work with ring buffers, shift a schedule, or move a window over fixed-size data without allocating. Interviewers ask it because the in-place requirement rules out the obvious copy, and the reversal trick is a nice example of finding a cheap sequence of operations that composes into the one we want. In this answer, we will recap what a right rotation is, start with the copy solution, build the three-reversal solution and see why it works, handle k larger than n, look at cyclic replacement and its tricky termination, and then rotate left.

1A Quick Recap of a Right Rotation

Rotating an array to the right by one step moves every element one position to the right, and the last element wraps around to the front. Rotating by k steps does that k times. For [1, 2, 3, 4, 5, 6, 7] and k = 3, the last three elements 5, 6, 7 move to the front and the others shift right: [5, 6, 7, 1, 2, 3, 4].

Another way to say it: the element at index i moves to index (i + k) % n. In place means we change the given array itself, and O(1) extra space means we may use a few variables but no second array whose size grows with n.

2The Simple Copy and Why It Is Not Enough

The direct solution writes each element to its new position in a new array and copies it back.

Java
static void rotateWithCopy(int[] nums, int k) {
int n = nums.length;
int[] out = new int[n]; // O(n) extra space
for (int i = 0; i < n; i++) {
out[(i + k) % n] = nums[i]; // index 0 (value 1) goes to index 3
}
System.arraycopy(out, 0, nums, 0, n);
}

It is correct and runs in O(n), and in many real programs it is the right choice because it is obvious. But it allocates a second array of the same size, which breaks the O(1) space requirement. Rotating one element at a time k times would use no extra space but costs O(n k), which is too slow when both are large.

3The Three Reversals

The trick is that reversing is cheap and in place, and three reversals together produce a rotation.

Java
static void rotate(int[] nums, int k) {
int n = nums.length;
if (n == 0) return; // nothing to rotate, and k % 0 would throw
k = k % n; // k = 10, n = 7 -> 3
reverse(nums, 0, n - 1); // [7, 6, 5, 4, 3, 2, 1]
reverse(nums, 0, k - 1); // [5, 6, 7, 4, 3, 2, 1]
reverse(nums, k, n - 1); // [5, 6, 7, 1, 2, 3, 4]
}
static void reverse(int[] a, int i, int j) {
while (i < j) {
int t = a[i]; a[i] = a[j]; a[j] = t; // swap the two ends
i++; j--;
}
}
Step throughRotating [1..7] right by 3
  1. k = k % n10 % 7 = 3, and n == 0 returns early
  2. Reverse the whole array[7, 6, 5, 4, 3, 2, 1]
  3. Reverse the first k[5, 6, 7, 4, 3, 2, 1]
  4. Reverse the remaining n - k[5, 6, 7, 1, 2, 3, 4]

4 steps in order. Play them, or step through them one at a time.

A B becomes reverse(B) reverse(A), then each block is turned back the right way.

Why does this work? A right rotation by k means the last k elements, call them block B, must come before the first n - k elements, block A. So we want to turn A B into B A. Reversing the whole array gives reverse(B) followed by reverse(A): the blocks are in the right order, but each block is backwards. Reversing each block on its own puts its elements back in their original order, and we are left with B A.

Each element is swapped at most twice, once in the full reversal and once in its block's reversal, so the total work is about n swaps: O(n) time. We only use the loop variables and one temporary, so extra space is O(1). Notice that when k becomes 0 after the modulo, reverse(nums, 0, -1) does nothing and the second and third calls undo the first, which is correct but wasted work, so a real version can also return early when k == 0.

4Handling k Larger Than the Array

Rotating seven elements by 7 brings every element back to where it started, so rotating by 10 is the same as rotating by 3. That is why we take k % n first. Without it, reverse(nums, 0, k - 1) would read past the end of the array and throw ArrayIndexOutOfBoundsException.

Two edge cases deserve a sentence in the interview. An empty array must return before the modulo, because k % 0 throws ArithmeticException. And if the contract allowed a negative k, meaning a left rotation, k % n would stay negative in Java, so Math.floorMod(k, n) turns -1 into n - 1, which is the same rotation.

5Cyclic Replacement and Its Tricky Loop

Another O(1) approach moves each element directly to its final place. We pick up the element at index 0, put it at (0 + k) % n, pick up the element that was there, put it at its own target, and continue until we return to the start. The catch is that when n and k share a divisor, this cycle comes back to the start before visiting every index. With n = 6 and k = 2, starting at 0 visits 0, 2, 4 and then returns to 0, leaving 1, 3 and 5 untouched.

Java
static void rotateCyclic(int[] nums, int k) {
int n = nums.length;
if (n == 0) return;
k %= n;
int moved = 0;
for (int start = 0; moved < n; start++) { // one cycle per start, gcd(n, k) cycles in total
int current = start, carried = nums[start];
do {
int next = (current + k) % n;
int t = nums[next]; nums[next] = carried; carried = t;
current = next;
moved++;
} while (current != start);
}
}

The fix is to count how many elements we have moved and start a new cycle at the next index until all n have moved. There are exactly gcd(n, k) cycles, each of length n / gcd(n, k). It is O(n) and O(1) space like the reversal method, but it is easier to get wrong, which is why the reversal method is usually the answer we write and cyclic replacement is the one we mention.

6Follow-up questions

How would you rotate left instead?

A left rotation by k is a right rotation by n - k, so the simplest answer is rotate(nums, n - k % n). Directly, we reverse the first k elements, reverse the rest, and then reverse the whole array; that turns A B into B A where A is now the first k elements. Both are O(n) time and O(1) space.

Does Java have a built-in rotation?

For a List, yes: Collections.rotate(list, distance) rotates in place, and a positive distance rotates to the right. It uses a reversal-based or a cycle-based algorithm depending on the list type and size. There is no rotate for primitive arrays, so for an int[] we write the method ourselves, or wrap it, which boxes every element.

Why not rotate one step at a time?

Because each one-step rotation moves all n elements, so k steps cost O(n k). With n = 1,000,000 and k = 500,000 that is 500 billion moves, while the reversal method does about a million swaps.

7Conclusion

In this answer, we saw that a right rotation by k is the same as rotation by k % n, that the copy solution is simple but uses O(n) space, and that three in-place reversals, the whole array, then the first k, then the rest, turn A B into B A in O(n) time with O(1) extra space. Cyclic replacement is another constant-space method, but it needs gcd(n, k) cycles, which is easy to get wrong, and a left rotation is either a right rotation by n - k or the same reversals in a different order.

What to say in the interviewAbout 100 seconds aloud

Open with

First I reduce k modulo n, because rotating seven elements by seven does nothing, so rotating by ten is rotating by three. I also return early for an empty array, since modulo zero would throw.

Then, if they want more

Then I do three reversals with a two-pointer swap. I reverse the whole array, which puts the last k elements at the front but backwards, then I reverse the first k elements and then the remaining n minus k, which puts each block back in its original order.

For 1 to 7 with k equal to 3, the whole reversal gives 7 6 5 4 3 2 1, the first three become 5 6 7, the rest become 1 2 3 4, and the result is 5 6 7 1 2 3 4.

The reason it works is that a rotation turns A B into B A, and reversing everything gives reverse B reverse A, so reversing each block fixes it. Every element is swapped at most twice, so it is O(n) time, and I only use a temporary variable, so it is O(1) space.

The simple alternative copies into a new array at index i plus k modulo n, which is clearer but O(n) space. I would also mention cyclic replacement, which moves each element straight to its target, but it needs a new cycle whenever n and k share a divisor, so it is easier to get wrong. For a left rotation I would rotate right by n minus k.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Write a Java method that rotates an int array to the right by k steps in place...": https://scrivio-interview-library.netlify.app/java/coding-problems/f0a51da0a4/