Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Stash `nums[0]`, shift `nums[i] = nums[i+1]` for all i, then put the stashed value in the last slot. O(n) time, O(1) space.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you preserve the clobbered value and avoid an out-of-bounds read.
Where candidates slip
Losing the head, or off-by-one at the upper bound.
How to stand out
Generalise to rotate-by-k via three reversals in O(1) space.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Shift with saved head
1Hint 1Explore hintHide hint
Which element gets overwritten first, and how do you preserve it?
2Hint 2Explore hintHide hint
Save nums[0], shift everything left by one, then place it at the end.
3Hint 3Explore hintHide hint
Watch the upper bound so you don't read nums[n].
A · Core idea
Rotating left by one slides every element down one index and wraps the head to the tail. Save the first element, shift, then drop it at the end.
Why save the head first. The shift nums[i] = nums[i+1] overwrites nums[0] on the very first step, so you must capture the original head before the loop, then place it in the last slot afterward. Skipping the save loses it.
Why the loop stops at n−2. The last write is nums[n-2] = nums[n-1]; going to nums[n-1] = nums[n] reads off the end. After the loop, nums[n-1] is set to the saved head.
Rotating by k. A left rotation by k is best done with the three-reversal trick: reverse the first k, reverse the rest, reverse the whole — O(n) time, O(1) space, no big temp array. Mentioning that generalisation is the standout move.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [1, 2, 3, 4]save
first = 1
first=1[2, 3, 4, 1]C · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(n) time, O(1) space — one shift per element with a single saved value — linear time, constant space.
E · Edge cases and pitfalls4nums = [5] — Single element — return unchanged.Explore edge casesHide edge cases
Edge cases handled
nums = [5]Single element — return unchanged.nums = [7, 8]Becomes [8, 7].
Pitfalls
- Overwriting nums[0] before saving it.
- Looping to the last index and reading nums[n] out of bounds.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic void rotateLeft(int[] nums) { if (nums.length <= 1) return; int first = nums[0]; for (int i = 0; i < nums.length - 1; i++) { nums[i] = nums[i + 1];Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Overwriting nums[0] before saving it.
- Looping to the last index and reading out of bounds (nums[n]).
Revise
Carry the pattern into the interview
Pattern to remember
Shift with saved head
Code pattern to memorize
t=a[0]; shift left; a[n-1]=t (k: three reversals)Rules to remember
- 1Save nums[0] first.
- 2Shift left in order.
- 3Drop saved value at the end.
Use this pattern when
- Cyclic shifts
- Rotations
Interview takeaway
Always snapshot the value an in-place shift will clobber.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Shift with saved head. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Rotate Array by kComing soonThree reversals: reverse[0..k), reverse[k..n), reverse all.
- Reverse an ArrayThe primitive behind the rotation trick.