23. Rotate Array
easyAsked at SnowflakeRotate an array to the right by k steps in-place. Snowflake asks this to test the reverse-three-times trick — an elegant O(1) space transformation that mirrors how their executor performs in-place reorderings on column chunks.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Snowflake loops.
- Glassdoor (2025-Q4)— Snowflake execution-engine team uses this as a follow-up to reverse-string.
- LeetCode Discuss (2025-08)— Reported at Snowflake new-grad screens.
Problem
Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
Constraints
1 <= nums.length <= 10^5-2^31 <= nums[i] <= 2^31 - 10 <= k <= 10^5
Examples
Example 1
nums = [1,2,3,4,5,6,7], k = 3[5,6,7,1,2,3,4]Example 2
nums = [-1,-100,3,99], k = 2[3,99,-1,-100]Approaches
1. Slice and concatenate
Copy the last k elements to the front using slicing.
- Time
- O(n)
- Space
- O(n)
function rotate(nums, k) {
k = k % nums.length;
const tail = nums.slice(-k);
nums.copyWithin(k, 0, nums.length - k);
for (let i = 0; i < k; i++) nums[i] = tail[i];
}Tradeoff: Linear time but O(n) auxiliary buffer for the tail. Violates the in-place spirit.
2. Reverse three times (optimal)
Reverse the whole array; reverse the first k; reverse the rest. Net effect: rotate right by k.
- Time
- O(n)
- Space
- O(1)
function rotate(nums, k) {
k = k % nums.length;
function reverse(l, r) {
while (l < r) {
[nums[l], nums[r]] = [nums[r], nums[l]];
l++; r--;
}
}
reverse(0, nums.length - 1);
reverse(0, k - 1);
reverse(k, nums.length - 1);
}Tradeoff: Three sequential reverses, in-place. Cache-friendly because each reverse is sequential.
Snowflake-specific tips
Snowflake interviewers want the reverse-three-times trick stated as 'rotate is equivalent to two reverses'. Bonus signal: relate to in-place column reorderings — when Snowflake's executor reorders rows by a sort key, it often does in-place permutations of column chunks via similar cache-friendly transformations.
Common mistakes
- Forgetting k = k % n — for k > n you'd loop pointlessly.
- Using a single-element-at-a-time rotate, which is O(n*k).
- Confusing rotate-right with rotate-left and reversing the wrong segments.
Follow-up questions
An interviewer at Snowflake may pivot to one of these next:
- Rotate the array left by k.
- Rotate a string (LC 796 Rotate String).
- Cyclic replacement variant of in-place rotate.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does triple-reverse work?
Rotating right by k is equivalent to: reverse everything, then reverse the first k, then reverse the rest. Algebraically, rotation is the composition of two reflections.
Why is this cache-friendly?
Each reverse is a sequential scan with two pointers. Modern CPUs prefetch sequential reads efficiently, so even three passes still beat random-access alternatives.
Practice these live with InterviewChamp.AI
Drill Rotate Array and other Snowflake interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →