‹ DS&A interview · Socratic
DSA · Arrays · #154

Rotate Array

Module 48 · difficulty 3/5·30:00starts on first keystroke

Given an integer array `nums`, rotate the array to the right by `k` steps, where `k` is non-negative. Implement `rotate(nums, k)`. The rotation must be done **in place** — mutate `nums` directly and return nothing (`undefined`). `k` may be larger than `nums.length`.

Examples
  • nums = [1,2,3,4,5,6,7], k = 3 [5,6,7,1,2,3,4]The last 3 elements wrap around to the front.
  • nums = [-1,-100,3,99], k = 2 [3,99,-1,-100]
  • nums = [1,2], k = 3 [2,1]k is reduced modulo length: 3 % 2 = 1.
Constraints
  • · 1 <= nums.length <= 10^5
  • · -2^31 <= nums[i] <= 2^31 - 1
  • · 0 <= k <= 10^5
  • · Mutate nums in place; return undefined.
  • · Aim for O(1) extra space.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.