Skip to main content

283. Move Zeroes

easyAsked at Intel

Move all zeros to the end of the array in-place while preserving the relative order of non-zero elements. Intel asks because the two-pointer single-pass solution minimizes writes — a property that maps directly to wear-leveling in flash storage Intel ships.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Intel loops.

  • Glassdoor (2026-Q1)Intel SWE phone-screen reports cite move-zeroes as a recurring two-pointer warm-up.
  • GeeksforGeeks (2025-08)Intel Interview Experience archives reference move-zeroes with the in-place + minimize-writes follow-up.
  • LeetCode discuss (2025-12)Intel-tagged in the LC company-frequency listing.

Problem

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.

Constraints

  • 1 <= nums.length <= 10^4
  • -2^31 <= nums[i] <= 2^31 - 1

Examples

Example 1

Input
nums = [0,1,0,3,12]
Output
[1,3,12,0,0]

Example 2

Input
nums = [0]
Output
[0]

Approaches

1. Two passes (brute)

First pass: copy non-zeros forward into a write index. Second pass: fill the remaining tail with zeros.

Time
O(n)
Space
O(1)
function moveZeroesTwoPass(nums) {
  let write = 0;
  for (let i = 0; i < nums.length; i++) {
    if (nums[i] !== 0) nums[write++] = nums[i];
  }
  while (write < nums.length) nums[write++] = 0;
}

Tradeoff: Linear time, constant space, but performs n writes always — every element is written exactly once whether it was zero or not. Compare to the swap version which only writes 2 * (#non-zeros).

2. Single-pass swap (optimal — minimizes writes)

Walk read and write pointers. When you see a non-zero, swap it with the slot at write and advance write. Skips writes when the value is already in place.

Time
O(n)
Space
O(1)
function moveZeroes(nums) {
  let write = 0;
  for (let read = 0; read < nums.length; read++) {
    if (nums[read] !== 0) {
      if (read !== write) {
        // swap
        const tmp = nums[read];
        nums[read] = nums[write];
        nums[write] = tmp;
      }
      write++;
    }
  }
}

Tradeoff: Same Big-O as two-pass but performs fewer writes when most elements are non-zero. Critical when writes are expensive — flash storage, memory-mapped IO, copy-on-write pages.

Intel-specific tips

Intel will ask 'what if writes are 1000x more expensive than reads?' as the follow-up. That's the lever for justifying the swap version. State: 'The swap version writes ~2 * (#non-zeros) cells. The two-pass version writes n cells. If most elements are non-zero, two-pass wastes writes. If most are zero, swap wastes (nearly nothing) — both are good. The swap version dominates when write cost matters.' That tradeoff articulation lands the systems-flavored senior signal.

Common mistakes

  • Swapping when read === write (no-op but unnecessary write). The `if (read !== write)` guard skips it; trivial but matters for the write-count argument.
  • Returning the array instead of mutating in-place — the problem requires mutation; returning is fine in some languages but the value isn't used.
  • Using extra storage (e.g., filter then concat) — defeats the in-place requirement.

Follow-up questions

An interviewer at Intel may pivot to one of these next:

  • Remove Duplicates from Sorted Array (LC 26) — same two-pointer pattern.
  • Remove Element (LC 27) — same again with arbitrary target.
  • Sort Colors (LC 75) — three-way partition; Dutch National Flag.
  • What if you needed to move zeros to the FRONT instead? (Walk backward with two pointers.)

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why does the swap version minimize writes?

It only writes when it has to (when a non-zero element isn't already at its destination). A non-zero element at index i ends up at write-position w, so it's written at most once. Zeros that get displaced are written into the slot they came from; if the source slot held a zero originally, no write happens because read === write at that point.

Is there an O(1)-write version?

Not in general — by the problem definition, the output differs from the input in zero positions, so you must write at least once per moved zero. The swap version is provably write-optimal for this output.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

Drill Move Zeroes and other Intel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →