Skip to main content

23. Rotate Array

easyAsked at Salesforce

Rotate an array to the right by k steps. Salesforce uses this to test the three-reverse trick that achieves O(1) extra space.

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

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Asked on Salesforce backend phone screens as an in-place transform warmup.
  • Blind (2025)Salesforce data-pipeline team uses rotation for ring-buffer alignment.

Problem

Given an integer array nums, rotate the array to the right by k steps, where k is non-negative. Solve it 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]

Example 2

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

Approaches

1. Copy to new array

Allocate a new array; place each nums[i] at (i + k) mod n.

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

Tradeoff: O(n) extra space. Violates the explicit O(1) requirement.

2. Three reversals

Reverse the whole array, reverse first k, reverse rest. Result: rotated array.

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

Tradeoff: True O(1) space. The three-reverse trick is the canonical in-place rotation algorithm.

Salesforce-specific tips

Salesforce specifically grades on whether you can derive the three-reverse trick (or remember it). Bonus signal: don't forget the k % n step — interviewers love to throw k = 10^5 with n = 7 to trip up candidates who skip the modulo.

Common mistakes

  • Forgetting k = k % n — leads to OOB or wasted work when k > n.
  • Reversing first k after reversing rest — gives wrong order.
  • Using unshift in a loop — O(n) per call, total O(n*k).

Follow-up questions

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

  • Rotate Image (LC 48) — 2D matrix rotation.
  • Rotate List (LC 61) — linked list rotation.
  • Find Minimum in Rotated Sorted Array (LC 153).

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 the three-reverse trick work?

Reversing the whole array brings the back to the front (in reverse order). Reversing the two halves separately restores the within-half order. The net effect is a rotation.

What if k is negative (left rotation)?

Same algorithm with k = ((-k % n) + n) % n. Or use a 'rotate left' variant that swaps the order of the reversals.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →