Rotate Array by One

easy

Given an integer array arr, rotate it by one position in the clockwise direction and return the result.

A clockwise rotation by one takes the last element and moves it to the front; every other element shifts one slot to the right. So arr = [1, 2, 3, 4, 5] becomes [5, 1, 2, 3, 4].

The array always has at least one element. A single-element array is unchanged by rotation.

Hints

Only one element actually needs to travel a long distance — which one, and where does it land?
The last element jumps to the front; everyone else just slides one step toward the back. Can you do those slides without allocating a second array?
Save the last element, then copy each element one slot to the right — but sweep from the back to the front so you never overwrite a value before reading it.

Common doubts

Clockwise here means the last element wraps around to index 0. Counter-clockwise (a left rotation) would instead send the first element to the back, turning [1,2,3,4,5] into [2,3,4,5,1].
Either is accepted as long as you return the correctly rotated array. The optimal approach mutates in place for O(1) space; the brute-force approach returns a fresh copy.

Interview follow-ups

Rotating by k clockwise moves the last k elements to the front. The classic O(n)-time, O(1)-space trick is the reversal algorithm: reverse the whole array, then reverse the first k and the remaining n-k parts (take k mod n first).
Mirror the logic: save arr[0], slide every element one slot to the left with a front-to-back sweep, then drop the saved first element at the end.

Fun facts

  • Rotating by one is just rotating by k with k = 1 — and rotating by n (the array's length) lands you right back on the original, so all rotations live on a cycle of length n.
  • The three-reversal rotation trick reappears in text editors (moving a block of lines), circular buffers, and round-robin schedulers.

Asked at

AmazonMicrosoftAdobeGoogle
Frequently Sometimes Occasionally
Example 1
Input: arr = [1, 2, 3, 4, 5]
Output: [5, 1, 2, 3, 4]
The last element 5 moves to the front and the rest shift right by one.
Example 2
Input: arr = [9, 8, 7, 6, 4, 2, 1, 3]
Output: [3, 9, 8, 7, 6, 4, 2, 1]
The last element 3 comes to the first position; everything else slides one slot right.
Constraints

- 1 <= arr.size <= 10^5 - 0 <= arr[i] <= 10^5

Solve this problem →