Move Zeroes
Problem
Given an integer array nums, rearrange it in place so that every 0 is moved to the end while all non-zero elements keep their original relative order. Do not allocate another array; the function returns nothing and modifies nums directly.
numshas between 1 and 30,000 elements.- Elements are arbitrary integers, positive or negative; only exact zeros move.
Examples
nums = [4, 0, 0, 9, 0, 2]→[4, 9, 2, 0, 0, 0]. The non-zeros4, 9, 2keep their order.nums = [0, 0]→[0, 0]. Nothing to move.nums = [5, 8]→[5, 8]. No zeros; the array must be left untouched.
Hints
- If you were allowed a second array, what would you copy into it and in what order? Can the input array itself serve as that destination?
- Keep an index that marks where the next non-zero belongs; everything before it is already in final position.
- Read/write two pointers: scan with
read, and whenevernums[read]is non-zero, swap it intonums[write]and advancewrite.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.