Rotate Array
A medium Arrays interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Arrays
- Sheets
- 2
- Core for
- 26 roles
The problem
Shift every element of the array k places to the right. Elements that fall off the end wrap around to the front, and k can be larger than the array's length.
Example 1
- Input
- [3,8,1,9,4,6,2], k=3
- Output
- [4,6,2,3,8,1,9]
Example 2
- Input
- [-5,-200,7,45], k=2
- Output
- [7,45,-5,-200]
Example 3
- Input
- [1], k=0
- Output
- [1]
Constraints
- 1 <= n <= 10^5
- -2^31 <= arr[i] <= 2^31-1
- 0 <= k <= 10^5
How to think about it
Updated 2026-09-29Rotating an array by k steps splits it into two blocks that swap their relative positions: the prefix of length n - k and the suffix of length k. Reversing each section individually orients their contents backwards, and reversing the entire array flips them back into their correct relative order in place.
Approaches, worst first
Auxiliary buffer indexing
time O(n) · space O(n)
Create a new array and place each element arr[i] into (i + k) % n. Simple and straightforward to write, but allocates a complete copy of the array instead of rearranging values in place.
Three-step block reversalWrite this one
time O(n) · space O(1)
First reduce k modulo n. Reverse the entire array from 0 to n - 1. Then reverse the first k elements from 0 to k - 1, and finally reverse the remaining n - k elements from k to n - 1. Every element moves to its designated spot without extra allocations.
Where people lose marks · 3
- Neglecting to reduce k modulo n when k >= n, causing an out-of-bounds index boundary during subarray reversal.
- k = 0 or k % n = 0 must leave the array intact without running invalid slice ranges.
- A single-element array where reversing boundaries overlap must terminate cleanly.
Full solution
Three-step block reversal: reverse the whole array, then each of the two blocks, all in place. It is O(n) time like the buffer copy but O(1) space, which is exactly the follow-up an interviewer asks after the obvious (i + k) % n version.
Python
def rotate(nums: list[int], k: int) -> None:
n = len(nums)
k %= n # k >= n wraps around; k % n == 0 leaves the array untouched
def reverse(lo: int, hi: int) -> None:
while lo < hi:
nums[lo], nums[hi] = nums[hi], nums[lo]
lo += 1
hi -= 1
reverse(0, n - 1) # whole array: the last k now sit first, but backwards
reverse(0, k - 1) # fix the order of the moved block
reverse(k, n - 1) # fix the order of the rest
JavaScript
function rotate(nums, k) {
const n = nums.length;
k %= n; // k >= n wraps around; k % n === 0 leaves the array untouched
const reverse = (lo, hi) => {
while (lo < hi) {
[nums[lo], nums[hi]] = [nums[hi], nums[lo]];
lo++;
hi--;
}
};
reverse(0, n - 1); // whole array: the last k now sit first, but backwards
reverse(0, k - 1); // fix the order of the moved block
reverse(k, n - 1); // fix the order of the rest
}
The theory behind it
Arrays — the ground this problem stands on. All Arrays problems
What Arrays is
An array is a row of fixed boxes laid side by side in computer memory, like numbered lockers in a hallway. Because each box occupies identical space and sits directly next to its neighbors, jumping to locker zero or locker ten thousand takes the exact same tiny fraction of time. Every box holds an item of the same type, addressed by an offset number called an index.
When to reach for it
Reach for an array when items arrive in a known sequence and need immediate retrieval by position number. Problems asking for running totals, prefix accumulations, cyclic rotations, or in-place rearrangements signal array mechanics. Whenever constraints require constant-time random lookups or contiguous cache scans across fixed collections, a flat sequence is the default container.
How the pattern works
Visualize a tape with zero-indexed slots stretching from start to end. Keep track of write and read cursors when modifying contents without allocating helper buffers. For running computations, maintain an invariant such as having processed all elements left of the current index while pending elements wait to the right. When modifying entries in place, consider scanning backwards from the end so unread data is not overwritten.
More Arrays problems
Target Roles
Core requirement for 26 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .