Skip to main content

24. Rotate Array

easyAsked at Datadog

Rotate an array to the right by k steps. Datadog asks this for the in-place reversal trick — same pattern as cyclically rotating a fixed-size ring buffer in their ingest pipeline.

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

Source citations

Public interview reports confirming this problem appears in Datadog loops.

  • Glassdoor (2026-Q1)Datadog phone screen — explicitly asks for O(1) space.
  • LeetCode Discuss (2025-09)Datadog tagged.

Problem

Given an integer array nums, rotate the array to the right by k steps, where k is non-negative. Try to come up with as many solutions as you can; there are at least three different ways to solve this problem. Could you do 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. Slice + concat

Take the last k, append the first n-k.

Time
O(n)
Space
O(n)
function rotate(nums, k) {
  k = k % nums.length;
  const tail = nums.slice(-k);
  const head = nums.slice(0, nums.length - k);
  for (let i = 0; i < nums.length; i++) nums[i] = (i < k ? tail[i] : head[i - k]);
}

Tradeoff: Allocates O(n). Datadog explicitly asks for O(1).

2. Triple-reverse in place (optimal)

Reverse the whole array; reverse the first k; reverse the rest.

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

Tradeoff: O(1) extra space, two passes worth of swaps. The reverse-reverse-reverse trick is the Datadog-canonical in-place rotation.

Datadog-specific tips

Datadog wants the in-place triple reverse, not slice+concat. They'll also push you to think about k > n (handled with k % n) and negative k (left rotation = right by n-k). Articulate WHY triple reverse works — it's not obvious unless you visualize.

Common mistakes

  • Forgetting k = k % n — fails on k > n.
  • Off-by-one in the reverse function — easy to write reverse(0, n) instead of reverse(0, n-1).
  • Using the cyclic-replacement approach without gcd handling — works but is harder to get right than triple reverse.

Follow-up questions

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

  • Rotate Image (LC 48) — 2D version (transpose + flip).
  • Cyclically rotate a linked list (LC 61).
  • Datadog-style: cyclically rotate a ring buffer of metric points.

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 triple-reverse work?

Reverse(all) puts everything backward. Reverse(first k) restores the order of the last k (now at the front). Reverse(rest) restores the order of the first n-k (now at the back).

What if k > n?

Take k = k % n. Rotating by n is a no-op.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →