Skip to main content

23. Rotate Array

easyAsked at Vercel

Rotate an array to the right by k steps in-place. Vercel asks this for the reverse-thrice trick — clean O(1) space pattern that catches anyone who hasn't internalized it.

By Alex Chen, 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.

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]

Example 2

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

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 grades for the reverse-thrice insight. Bonus signal: stating the k %= n normalization first and noting that k = 0 is a no-op. They may follow up with 'why does reverse-thrice work?' — be ready to justify the algebraic shift.

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.

Practice these live with InterviewChamp.AI

Drill Rotate Array and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →