Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
Example 1:
Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4]
Example 2:
Input: nums = [-1,-100,3,99], k = 2 Output: [3,99,-1,-100] Explanation: rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100]
Constraints:
1 <= nums.length <= 105-231 <= nums[i] <= 231 - 10 <= k <= 105Follow up:
O(1) extra space?Problem summary: Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Two Pointers
[1,2,3,4,5,6,7] 3
[-1,-100,3,99] 2
rotate-list)reverse-words-in-a-string-ii)make-k-subarray-sums-equal)maximum-number-of-matching-indices-after-right-shifts)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #189: Rotate Array
class Solution {
private int[] nums;
public void rotate(int[] nums, int k) {
this.nums = nums;
int n = nums.length;
k %= n;
reverse(0, n - 1);
reverse(0, k - 1);
reverse(k, n - 1);
}
private void reverse(int i, int j) {
for (; i < j; ++i, --j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}
}
// Accepted solution for LeetCode #189: Rotate Array
func rotate(nums []int, k int) {
n := len(nums)
k %= n
reverse := func(i, j int) {
for ; i < j; i, j = i+1, j-1 {
nums[i], nums[j] = nums[j], nums[i]
}
}
reverse(0, n-1)
reverse(0, k-1)
reverse(k, n-1)
}
# Accepted solution for LeetCode #189: Rotate Array
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
def reverse(i: int, j: int):
while i < j:
nums[i], nums[j] = nums[j], nums[i]
i, j = i + 1, j - 1
n = len(nums)
k %= n
reverse(0, n - 1)
reverse(0, k - 1)
reverse(k, n - 1)
// Accepted solution for LeetCode #189: Rotate Array
impl Solution {
pub fn rotate(nums: &mut Vec<i32>, k: i32) {
let n = nums.len();
let k = (k as usize) % n;
nums.reverse();
nums[..k].reverse();
nums[k..].reverse();
}
}
// Accepted solution for LeetCode #189: Rotate Array
/**
Do not return anything, modify nums in-place instead.
*/
function rotate(nums: number[], k: number): void {
const n: number = nums.length;
k %= n;
const reverse = (i: number, j: number): void => {
for (; i < j; ++i, --j) {
const t: number = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
};
reverse(0, n - 1);
reverse(0, k - 1);
reverse(k, n - 1);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.