Move Zeroes: In Place Partitioning With a Write Pointer
"Move Zeroes" is a Easy-level staple in Meta, Bloomberg, Microsoft loops, and it is a textbook fit for the Two Pointers pattern — one of the nine patterns that cover the bulk of what FAANG coding interviews actually test. Part 5 of 12 in the Two Pointers arc.
The Problem
Given an integer array nums, move all zeroes to the end while maintaining the relative order of the non-zero elements. Do it in place, without making a copy, and ideally with a minimal number of writes.
Input: nums = [0, 1, 0, 3, 12]
Output: [1, 3, 12, 0, 0]
Recognizing the Two Pointers Pattern
Two pointers means walking a sequence with two indices that move based on a comparison — from both ends inward, or slow-and-fast in one direction. It turns brute-force pair scans that cost O(n²) into a single O(n) pass, and it is usually the intended answer whenever the input is sorted or can be sorted cheaply.
Stable in-place partitioning — keep-these in front, move-those behind — is the reader-writer pattern again. Any solution that allocates a second list answers a different, easier question, and Meta interviewers will say exactly that.
The Approach
write marks where the next non-zero belongs. The reader scans; on a non-zero it swaps into write and advances it. Zeroes naturally migrate backward as swaps pass over them, and non-zero order is preserved because the reader encounters them in order.
The swap formulation beats copy-then-fill-zeroes when writes are expensive — a nice systems-flavored remark for a senior loop.
Python Solution
def move_zeroes(nums: list[int]) -> None:
"""Stable partition: non-zeroes forward, zeroes to the back."""
write = 0
for read in range(len(nums)):
if nums[read] != 0:
if read != write:
nums[write], nums[read] = nums[read], nums[write]
write += 1
Complexity
- Time: O(n) — single reader pass with constant work per element
- Space: O(1) — swaps only, no auxiliary array
Interview Tips and Follow-Ups
- Count your writes when asked to minimize them: the swap version writes only when needed thanks to the
read != writeguard. - Generalize on request: partition by any predicate (evens forward, odds back) with the same skeleton.
- If stability is not required, two converging pointers do it with even fewer writes — ask whether order matters before coding.
More Two Pointers problems — and the other eight patterns — live in the Technical Interview category. Drill the pattern, not the problem: that is the entire thesis of this series.
Keep reading
Backspace String Compare Backwards in Constant Space
Compare typed strings with backspaces in O(1) space, scanning backwards. Python solution and complexity analysis for the two pointers interview pattern.
Container With Most Water and the Greedy Pointer Proof
Maximize trapped area and prove the greedy pointer move is safe. Python solution and complexity analysis for the two pointers interview pattern.
Remove Duplicates In Place: The Reader Writer Pointer Trick
Dedupe a sorted array in place with a reader and a writer index. Python solution and complexity analysis for the two pointers interview pattern.
Newsletter
New posts, straight to your inbox
One email per post. No spam, no tracking pixels, unsubscribe anytime.
Comments
- No comments yet. Be the first.