Skip to content
Decoder

Rotate Array

Medium

Problem

Given a list nums, rotate it to the right by `k` steps in place: each step moves the last element to the front. k may be larger than the length. As a follow-up, use O(1) extra space.

Example 1

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

Example 2

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

The function returns nothing; the tests inspect nums after the call.

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.