- Interview Questions
- /
- Problems
- /
- Move Zeroes
Move Zeroes
Shift every zero in an array to the end while keeping the other values in their original order, in place. A clean introduction to the read/write two-pointer pattern. Solve it below in Python or JavaScript, then read the walkthrough.
Problem
Given an integer array nums, move all of its 0s to the end. The non-zero values must keep the same relative order they started in.
Do this in place: modify nums directly rather than building and returning a new array. Your function doesn't need to return anything; the array is checked after it runs.
Example 1
- Input
- nums = [0, 4, 0, 7, 2]
- Output
- [4, 7, 2, 0, 0]
Example 2
- Input
- nums = [9, 0, 0, -3]
- Output
- [9, -3, 0, 0]
Negative numbers are non-zero, so -3 stays after 9.
Example 3
- Input
- nums = [5, 6, 1]
- Output
- [5, 6, 1]
No zeros, so nothing moves.
Constraints
- 1 ≤ nums.length ≤ 10⁴
- -2³¹ ≤ nums[i] ≤ 2³¹ - 1
Practice
Runs 12 tests in your browser. Python loads on the first run (about 6 MB, then cached). Press ⌘/Ctrl + Enter to run.
Get asked Move Zeroes in a real interview?
Interview Solver gives you a working solution live, during your actual coding interview. Invisible to screensharing.
How to solve Move Zeroes
Pattern: Read/write two pointers
Think of the array as two zones: a "finished" prefix holding the non-zero values in order, and everything after it. A write pointer marks where the next non-zero value belongs; a read pointer scans every element.
Whenever the read pointer finds a non-zero value, it belongs at the write position, so swap it there and advance the write pointer. Because the write pointer never passes the read pointer, everything between them is a zero that has already been seen, and swapping pushes one of those zeros back. Non-zero values are placed in the order they're read, which keeps them stable.
Approach 1: Copy into a new array
Collect the non-zero values into a new list, append the right number of zeros, then copy back into nums. Easy and stable, but it uses O(n) extra memory, which the in-place requirement is meant to avoid.
Time O(n) · Space O(n)
Approach 2: Compact, then fill
One pass writes each non-zero value to nums[write++], overwriting as it goes. A second loop sets everything from write to the end to 0. In place and stable; it writes to every zero slot at the end even when nothing moved.
Time O(n) · Space O(1)
Approach 3: Single pass with swaps
Keep write = 0. For each index read:
- If
nums[read]is non-zero, swapnums[read]andnums[write], then incrementwrite.
When the array starts with non-zeros, read == write and the swap is a harmless self-swap. Zeros collect behind write automatically, so no second pass is needed.
Time O(n) · Space O(1)
| Approach | Time | Space |
|---|---|---|
| Copy into a new array | O(n) | O(n) |
| Compact, then fill | O(n) | O(1) |
| Single pass with swaps | O(n) | O(1) |
Solution code
Try the practice editor first. Both solutions pass every test above.
Move Zeroes solution in Python
def move_zeroes(nums: list[int]) -> None:
write = 0 # next slot for a non-zero value
for read in range(len(nums)):
if nums[read] != 0:
nums[write], nums[read] = nums[read], nums[write]
write += 1Move Zeroes solution in JavaScript
function moveZeroes(nums) {
let write = 0; // next slot for a non-zero value
for (let read = 0; read < nums.length; read++) {
if (nums[read] !== 0) {
const tmp = nums[write];
nums[write] = nums[read];
nums[read] = tmp;
write++;
}
}
}Common mistakes
- Building and returning a new array. The grader checks the original array after the call.
- Removing zeros with splice/pop inside a loop, which is O(n²) and easy to get off-by-one when indices shift.
- Sorting, which loses the original order of the non-zero values.
- In the compact-then-fill version, forgetting the fill step and leaving stale values at the end.
What interviewers look for
State the invariant out loud: "everything before write is the non-zero values in order; everything between write and read is zeros." Interviewers want to see that you can name what each pointer means, because that's the reusable skill behind Remove Element, Remove Duplicates and partitioning problems.
If asked to minimize writes, mention that the swap version only touches the array when a non-zero value has to move, while compact-then-fill always rewrites the tail.
Follow-up questions
How would you move zeros to the front instead?
Scan from right to left with the write pointer starting at the last index, placing non-zero values from the back. The non-zeros stay in order and zeros collect at the front.
What if order of the non-zero values didn't matter?
Use two pointers from opposite ends: when the left finds a zero and the right finds a non-zero, swap them. This can take fewer swaps but isn't stable.
How does this generalize?
Replace "is non-zero" with any keep condition and you get stable in-place filtering, the same pattern used to remove a given value or de-duplicate a sorted array.
Companies that ask Move Zeroes
Move Zeroes is tagged by 116 companies in our interview dataset. The score shows how often it comes up at each company.
- Solo Funds100%
- Abbvie100%
- LTI100%
- Chewy100%
- Harman International95%
- Sigmoid91.7%
- Epam Systems89.2%
- Wixcom87.3%
- Weave85%
- Dolby Laboratories84%
- Project4482%
- Jb Hunt82%
- Ericsson81%
- Here Technologies80%
- Valon79%
- Skyworks Solutions79%
- Netapp78.8%
- Latitude Ai78%
- Blackhawk Network77%
- Grafana76%
- Embark Studios76%
- Qualcomm75.6%
- Vk75.1%
- Siriusxm75%
and 92 more.
Practice next
Get live help in the real interview
Working solutions in real time during your actual interview. Invisible to screensharing.
Start Free TrialNo credit card required