Skip to main content

23. Rotate Array

easyAsked at Vercel

Shift every element of an array k positions rightward, wrapping the overflow back to the front — in place, in linear time. Vercel candidates report this easy as a pattern-recognition gate: the reverse-three-times trick (whole array, then each segment) solves it in O(1) space with almost no code, and interviewers use it to separate candidates who have internalized the trick from those who reach for splice loops or a second array.

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2025-Q4)Vercel screen; in-place O(1) expected.
  • LeetCode Discuss (2026-Q1)Listed in Vercel screen pool.

Problem

Given an integer array nums, rotate the array to the right by k steps, where k is non-negative. k may exceed the array length (normalize with k %= n), and the expected solution mutates nums in place with O(1) extra space.

Constraints

  • 1 <= nums.length <= 10^5
  • -2^31 <= nums[i] <= 2^31 - 1
  • 0 <= k <= 10^5

Examples

Example 1

Input
nums = [1,2,3,4,5,6,7], k = 3
Output
[5,6,7,1,2,3,4]

Explanation: The last three elements [5,6,7] wrap around to the front; everything else slides right. Reverse-thrice: [7,6,5,4,3,2,1] → [5,6,7,4,3,2,1] → [5,6,7,1,2,3,4].

Example 2

Input
nums = [-1,-100,3,99], k = 2
Output
[3,99,-1,-100]

Explanation: Rotating by exactly half the length swaps the two halves — a handy sanity case because the answer is easy to eyeball.

Approaches

1. Auxiliary array

Create a new array; for each index i, copy nums[i] to (i+k) % n in the new array.

Time
O(n)
Space
O(n)
function rotate(nums, k) {
  const n = nums.length;
  k %= n;
  const out = new Array(n);
  for (let i = 0; i < n; i++) out[(i + k) % n] = nums[i];
  for (let i = 0; i < n; i++) nums[i] = out[i];
}

Tradeoff: Violates in-place. Mention only as the starting point.

2. Reverse three times (optimal)

Reverse the whole array. Reverse the first k elements. Reverse the rest. The net effect is a k-step rotation.

Time
O(n)
Space
O(1)
function rotate(nums, k) {
  const n = nums.length;
  k %= n;
  const rev = (l, r) => {
    while (l < r) {
      [nums[l], nums[r]] = [nums[r], nums[l]];
      l++; r--;
    }
  };
  rev(0, n - 1);
  rev(0, k - 1);
  rev(k, n - 1);
}

Tradeoff: O(1) extra space, O(n) time. The reverse-thrice trick is the canonical answer. The k %= n at the top handles k larger than n.

Vercel-specific tips

Vercel candidates report the grading centers on the reverse-thrice insight arriving fast and clean. Two cheap senior signals: normalize k %= n BEFORE anything else (and say why — k up to 10^5 can exceed n, and k = n is a no-op), and answer the inevitable 'why does it work?' with the algebra rather than hand-waving: a right-rotation moves the last k elements to the front; reversing the whole array puts them in front but backwards, and the two segment reversals restore each part's internal order. Volunteer the cyclic-replacement variant by name as the single-pass alternative.

Common mistakes

  • Forgetting k %= n — TLE on huge k.
  • Calling rev(0, k) instead of rev(0, k-1) — off-by-one.
  • Using nums.splice or unshift in a loop — O(n*k) and slow.

Follow-up questions

An interviewer at Vercel may pivot to one of these next:

  • Rotate to the left by k — symmetric.
  • Find the rotation index of a rotated sorted array (LC 33).
  • Cyclic-replacement variant — single pass with start-position tracking.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why does reverse-thrice work?

Reversing the whole array flips both halves. Reversing each half restores their internal order — but now the back half is in front and the front half is in back, which is exactly a rotation by k.

When would the cyclic-replacement version be preferred?

When you want a single pass without three reverse calls. It's tricky because gcd(n, k) cycles must be tracked. Reverse-thrice is the interview-friendly version.

Why is the auxiliary-array version still worth mentioning?

It states the rotation as a pure index mapping — out[(i + k) % n] = nums[i] — which is the cleanest way to communicate WHAT a rotation is before optimizing HOW. Naming the mapping first, then deriving the in-place trick, reads as structured thinking rather than memorization.

Companies that also ask Rotate Array