Quick revision
The short answer
First we normalise k with k % n, returning early for an empty array or k == 0, so a rotation by 7 on five elements becomes a rotation by 2. Then we reverse the whole array, reverse the first k elements, and reverse the remaining n - k elements: {1,2,3,4,5} becomes {5,4,3,2,1}, then {4,5,3,2,1}, then {4,5,1,2,3}. A small reverse(a, i, j) helper swaps from both ends, so the method runs in O(n) time with O(1) extra space.
What to remember
In this answer, we rotated an int[] to the right by k in place. We normalised k with k % n, returning early for an empty array or k == 0, and handled negative k explicitly. Then we reversed the whole array, the first k elements and the remaining n - k elements, which gives O(n) time and O(1) extra space, compared with the simpler copy-based version that needs O(n) space. We covered the tests for empty arrays, k = 0, k = n, k > n and negative k, and saw that rotating left uses the same reversals in the opposite order.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
First we normalise k with k % n, returning early for an empty array or k == 0, so a rotation by 7 on five elements becomes a rotation by 2. Then we reverse the whole array, reverse the first k elements, and reverse the remaining n - k elements: {1,2,3,4,5} becomes {5,4,3,2,1}, then {4,5,3,2,1}, then {4,5,1,2,3}. A small reverse(a, i, j) helper swaps from both ends, so the method runs in O(n) time with O(1) extra space.
Why this question is asked
Rotating an array shows up in circular buffers, scheduling (moving the next shift to the front), and as a building block in string and matrix problems. Interviewers like it because the simple solution uses extra memory, and the O(1) space solution needs a small insight that we should be able to explain, not just remember. In this answer, we will recap what rotating to the right means, write the simple copy-based version, build the three-reversal version and see why it works, handle large and negative k, and list the tests that prove it. We will also answer the follow-up about rotating left.
1A Quick Recap: What Rotating Right Means
Rotating right by k moves every element k positions towards the end, and elements that fall off the end wrap around to the front. With {1,2,3,4,5} and k = 2, the last two elements, 4 and 5, wrap to the front: {4,5,1,2,3}. Element at index i ends up at index (i + k) % n.
Rotating by n, the length, gives back the original array, so rotating by k is the same as rotating by k % n. For n = 5, k = 7 is the same as k = 2. That observation is the first step of any solution.
2The Simple Version With a Copy
The easiest correct solution copies the array into a temporary one and writes each element to its new position. Using System.arraycopy, it takes two calls.
This runs in O(n) time and is very clear, but it needs O(n) extra space for tmp. For a 100 million element array that is another 400 MB, which is exactly what the question asks us to avoid.
3The Three-Reversal Version
The O(1) space solution relies on one observation. After rotating right by k, the array is the last k elements followed by the first n - k elements, each block still in its original order. Reversing the whole array puts the two blocks in the right place but with each block backwards. Reversing each block again restores its order.
| Step | Array after it | Why |
|---|---|---|
| start | 1 2 3 4 5 | blocks: [1 2 3] and [4 5] |
| reverse all | 5 4 3 2 1 | blocks swapped, each one backwards |
| reverse first k = 2 | 4 5 3 2 1 | first block back in order |
| reverse last n - k = 3 | 4 5 1 2 3 | second block back in order |
The reverse helper swaps the two ends and moves inwards until they meet, so it touches each element of its range once. The three calls together swap about n elements in total, so the time is O(n), and the only extra memory is the variable t, so the space is O(1).
Notice that the normalisation must come before the reversals. Without k % n, a k of 7 would make reverse(a, 0, 6) run past the end of a five-element array and throw ArrayIndexOutOfBoundsException.
4Handling k Larger Than the Length, and Negative k
k % n handles any k larger than n. Java's % keeps the sign of the left operand, so for a negative k, such as -1, k % n is -1, not 4. If the method allows negative k with the meaning rotate left, adding n when the result is negative converts it: -1 + 5 = 4, and rotating right by 4 is the same as rotating left by 1. If negative k is not part of the contract, it is cleaner to reject it with an IllegalArgumentException than to guess.
The early return for n == 0 matters for a different reason: k % 0 throws ArithmeticException, so we must not reach the modulo with an empty array.
5Tests That Prove It
The edge cases are where rotation code breaks, so a good test set covers each of them.
A property-based test adds confidence cheaply: for random arrays and random k, rotateRight should give the same result as rotateWithCopy, and rotating right by k and then left by k should return the original array.
6Follow-up questions
How would you rotate left instead?
Rotating left by k is the same as rotating right by n - k, so the simplest answer is rotateRight(a, n - (k % n)). With reversals directly, we reverse the first k elements, then the remaining n - k, and then the whole array: {1,2,3,4,5} with k = 2 becomes {2,1,3,4,5}, then {2,1,5,4,3}, then {3,4,5,1,2}. It is the same three reversals in the opposite order.
Is there another O(1) space approach?
Yes, the cyclic replacement method moves each element directly to its final position, following cycles of length n / gcd(n, k). It does exactly n moves instead of about 2n swaps, but it is harder to get right, so the reversal method is usually preferred in interviews and in production code.
Would you use Collections.rotate in real code?
For a List, yes: Collections.rotate(list, k) does exactly this, including negative k. For a primitive int[], there is no JDK method, so we write the reversal version or use System.arraycopy if the extra memory is acceptable.
7Conclusion
In this answer, we rotated an int[] to the right by k in place. We normalised k with k % n, returning early for an empty array or k == 0, and handled negative k explicitly. Then we reversed the whole array, the first k elements and the remaining n - k elements, which gives O(n) time and O(1) extra space, compared with the simpler copy-based version that needs O(n) space. We covered the tests for empty arrays, k = 0, k = n, k > n and negative k, and saw that rotating left uses the same reversals in the opposite order.
Open with
First I'd normalise k: if the array is empty or k is zero I return, and otherwise I take k % n, because rotating by the length gives the same array, so 7 on five elements is really 2.
Then, if they want more
If negative k is allowed, I add n when the remainder is negative, since Java's modulo keeps the sign. The simple solution is to copy the array and use two System.arraycopy calls, but that needs O(n) extra space. For O(1) space, I use three reversals.
After a right rotation, the array is the last k elements followed by the first n - k. Reversing the whole array puts those two blocks in the right place, but each one backwards, so I reverse the first k elements and then the remaining n - k to restore their order.
For 1 to 5 with k = 2, that goes 5 4 3 2 1, then 4 5 3 2 1, then 4 5 1 2 3. The reverse helper just swaps a[i] and a[j] while i is less than j. It's O(n) time and constant space.
I'd test empty, k = 0, k = n, k greater than n and negative k. To rotate left, I'd rotate right by n - k, or do the same reversals in the other order.
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 documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 method that rotates an int[] to the right by k positions in place with O(1)...": https://scrivio-interview-library.netlify.app/java/arrays/95473a6b39/