Algorithm to rotate an array in linear time
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Rotating an array means shifting its elements by k positions while preserving relative order. The classic in-place linear-time solution is the reversal algorithm, which runs in O(n) time and uses O(1) extra space.
Right Rotation With Three Reversals
To rotate right by k positions:
- reverse the whole array
- reverse the first
kelements - reverse the remaining elements
For example, starting with:
Rotate right by 3:
- reverse all:
[7, 6, 5, 4, 3, 2, 1] - reverse first
3:[5, 6, 7, 4, 3, 2, 1] - reverse rest:
[5, 6, 7, 1, 2, 3, 4]
That is the final rotated array.
Python Implementation
Here is a complete in-place implementation:
This is linear time because each element is touched only a constant number of times across the three reversal passes.
Left Rotation Variant
Left rotation is the mirror image. One clean implementation is:
That produces:
Why Not Use Extra Arrays
If extra memory is allowed, many high-level languages offer simpler syntax such as slicing:
This is still linear time, but it allocates additional memory. The in-place reversal algorithm is preferred when the constraint explicitly says:
- linear time
- constant extra space
That is the main reason the reversal method appears so often in interview and systems-programming questions.
Another Linear-Time Option: Juggling
There is also a juggling algorithm based on cycle decomposition and the greatest common divisor. It is linear and in-place as well, but it is usually harder to read and easier to get wrong.
For most practical discussions, the reversal algorithm is the best tradeoff between:
- performance
- simplicity
- ease of verification
So unless you have a very specific reason to prefer cycle-based movement, reversal is usually the clearer answer.
Edge Cases Matter
A robust rotation implementation should handle:
- empty arrays
- one-element arrays
- '
k = 0' - '
klarger than the array length' - negative or invalid input rules if the API allows them
This is why normalizing k with k %= n is so important. Rotating by the array length should do nothing, and rotating by a larger number should wrap around correctly.
Quick Test Harness
A small test makes off-by-one mistakes easier to catch:
Common Pitfalls
The most common pitfall is forgetting to reduce k modulo the array length before rotating.
Another mistake is mixing up the left-rotation and right-rotation reversal orders. The steps are similar, but not identical.
A third issue is using slicing or temporary arrays when the problem explicitly requires constant extra space.
Finally, developers sometimes get the segment boundaries wrong during reversal, especially around k - 1 and the split between the two parts.
Summary
- The reversal algorithm rotates an array in linear time with constant extra space.
- Right rotation uses three reversals: whole array, first
k, then the rest. - Left rotation uses the mirrored three-reversal pattern.
- Normalize
kwith modulo arithmetic before rotating. - The reversal method is usually the clearest in-place solution for this problem.

