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.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.
Example 1:
Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0]
Example 2:
Input: nums = [0] Output: [0]
Constraints:
1 <= nums.length <= 104-231 <= nums[i] <= 231 - 1Problem summary: Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers
[0,1,0,3,12]
[0]
remove-element)apply-operations-to-an-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #283: Move Zeroes
class Solution {
public void moveZeroes(int[] nums) {
int k = 0, n = nums.length;
for (int i = 0; i < n; ++i) {
if (nums[i] != 0) {
int t = nums[i];
nums[i] = nums[k];
nums[k++] = t;
}
}
}
}
// Accepted solution for LeetCode #283: Move Zeroes
func moveZeroes(nums []int) {
k := 0
for i, x := range nums {
if x != 0 {
nums[i], nums[k] = nums[k], nums[i]
k++
}
}
}
# Accepted solution for LeetCode #283: Move Zeroes
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
k = 0
for i, x in enumerate(nums):
if x:
nums[k], nums[i] = nums[i], nums[k]
k += 1
// Accepted solution for LeetCode #283: Move Zeroes
impl Solution {
pub fn move_zeroes(nums: &mut Vec<i32>) {
let mut k = 0;
let n = nums.len();
for i in 0..n {
if nums[i] != 0 {
nums.swap(i, k);
k += 1;
}
}
}
}
// Accepted solution for LeetCode #283: Move Zeroes
/**
Do not return anything, modify nums in-place instead.
*/
function moveZeroes(nums: number[]): void {
let k = 0;
for (let i = 0; i < nums.length; ++i) {
if (nums[i]) {
[nums[i], nums[k]] = [nums[k], nums[i]];
++k;
}
}
}
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: 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.