23. Rotate Array
easyAsked at PlaidRotate an array to the right by k steps in-place. Plaid asks this because shifting a circular buffer of recent transactions by a rotation amount has the same shape.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Plaid loops.
- Glassdoor (2025)— Plaid SWE I OA.
- LeetCode Discuss (2026)— Plaid intro.
Problem
Given an integer array nums, rotate the array to the right by k steps, where k is non-negative. Follow up: try doing it in-place with O(1) extra space.
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. Rotate one step at a time, k times
Pop the last element and unshift it onto the front, k times.
- Time
- O(n*k)
- Space
- O(1)
function rotate(nums, k) {
k %= nums.length;
for (let i = 0; i < k; i++) {
nums.unshift(nums.pop());
}
}Tradeoff: TLE for large k. Don't ship this. Also unshift is O(n).
2. Triple reverse in-place
Reverse the entire array, then reverse [0..k-1], then reverse [k..n-1].
- Time
- O(n)
- Space
- O(1)
function rotate(nums, k) {
k %= nums.length;
function rev(lo, hi) {
while (lo < hi) { [nums[lo], nums[hi]] = [nums[hi], nums[lo]]; lo++; hi--; }
}
rev(0, nums.length - 1);
rev(0, k - 1);
rev(k, nums.length - 1);
}Tradeoff: Three reversals = linear time, in-place. The k %= n step is critical or large k still loops past the end.
Plaid-specific tips
Plaid grades this on the triple-reverse trick because it shows you've internalized the in-place rotation pattern. Bonus signal: derive why three reversals work — reversing the whole then reversing the two halves restores order inside each block while swapping their positions.
Common mistakes
- Forgetting k %= n — pointless work or out-of-bounds.
- Allocating a new array — fails the in-place constraint.
- Using unshift in a loop — each unshift is O(n).
Follow-up questions
An interviewer at Plaid may pivot to one of these next:
- Rotate by k positions to the left instead.
- Rotate a 2D matrix by 90 degrees (LC 48).
- Rotate a singly-linked list (LC 61).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does triple-reverse work?
After reversing the whole array, the last k elements move to the front but in reverse order. Reversing each block restores their original order, placing them at the front.
Why does k %= n matter?
k larger than n is the same rotation as k mod n. Without the mod, you'd do extra wasted passes.
Practice these live with InterviewChamp.AI
Drill Rotate Array and other Plaid interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →