Skip to main content

24. Rotate Array

mediumAsked at Workday

Rotate an array to the right by k steps. Workday uses this to test in-place transformation discipline — same shape as shifting a payroll cycle's days when a holiday lands mid-period.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Workday loops.

  • Glassdoor (2025)Workday SDE2 phone screen.

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
  • Follow-up: Could you do it in-place with O(1) extra space?

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. Extra array

Copy nums[i] -> result[(i + k) % n]. Copy result back.

Time
O(n)
Space
O(n)
const n = nums.length;
const r = new Array(n);
for (let i=0;i<n;i++) r[(i+k)%n] = nums[i];
for (let i=0;i<n;i++) nums[i] = r[i];

Tradeoff: Easy but O(n) extra space — fails the follow-up.

2. Three reverses

Reverse the whole array, reverse the first k, reverse the rest. (k mod n first.)

Time
O(n)
Space
O(1)
function rotate(nums, k) {
  const n = nums.length;
  k = k % n;
  function reverse(i, j) {
    while (i < j) { [nums[i], nums[j]] = [nums[j], nums[i]]; i++; j--; }
  }
  reverse(0, n - 1);
  reverse(0, k - 1);
  reverse(k, n - 1);
}

Tradeoff: O(1) space. The trick: reversing the whole array puts the would-be tail at the head; then reversing each half restores intra-segment order.

Workday-specific tips

Workday wants the in-place version. State 'k mod n first' explicitly — otherwise you'd do extra work for k > n. The three-reverse trick is what they're testing.

Common mistakes

  • Forgetting k = k % n — k can exceed n.
  • Reversing in the wrong order — must be whole, then [0, k-1], then [k, n-1].
  • Off-by-one on reverse bounds (using k instead of k-1).

Follow-up questions

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

  • Rotate by k to the LEFT — same idea, reversed.
  • Rotate a string (LC 796 — substring of doubled string).
  • Rotate a 2D matrix (LC 48).

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 three reverses?

Reversing the whole array swaps suffix with prefix relative to position k. Each half is now in reverse order, so reversing each restores original ordering.

Cyclic-replace alternative?

Yes — start at index 0, place its value at (0+k)%n, place THAT value at (k+k)%n, until you cycle back. Use gcd(n,k) loops. Works but trickier to write correctly.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →