283. Move Zeroes
Asked at Meta, Microsoft, Apple, Walmart
Difficulty
Easy
Time
O(n)
Space
O(1)
Pattern
Array
Companies
4
Problem
You are given an integer array nums. Rearrange it so that all the zeros sit at the end, and all the non-zero values keep their original relative order.
For example, if the non-zero values appear as 1, 3, 12 in the input, they must still appear as 1, 3, 12 in that same order in the output. Only the zeros move.
You must do this in place — modify the given array directly. Do not build a new array or return a copy. The function just changes nums and returns nothing.
Move zeros to the back, keep order
Constraints
- ●
1 <= nums.length <= 10^4 - ●
-2^31 <= nums[i] <= 2^31 - 1
Examples
nums = [0,1,0,3,12][1,3,12,0,0]nums = [0][0]Asked At
4 companies| Company | Difficulty | |
|---|---|---|
| Meta | Easy | View all Meta questions → |
| Microsoft | Easy | View all Microsoft questions → |
| Apple | Easy | View all Apple questions → |
| Walmart | Easy | View all Walmart questions → |
How to Think About It
Use two pointers: a slow pointer (write position) and a fast pointer (scanner). The slow pointer tracks where the next non-zero element should go.
Why swap instead of shift: shifting all elements right is O(n^2). Swapping two elements is O(1) per operation. Total: O(n).
Alternative two-pointer approach: slow pointer only advances when a non-zero is found. Fast pointer always advances. When both are at non-zeros, they might be the same element, but that is harmless.
Edge cases: all zeroes (array unchanged), no zeroes (array unchanged), single element (unchanged).
Optimal Approach
Step 1: Initialize slow = 0.
Step 2: Iterate fast from 0 to n-1:
- If nums[fast] != 0:
- Swap nums[slow] and nums[fast]
- Increment slow
Step 3: The array is modified in-place.
The slow pointer always points to the position where the next non-zero should go. Everything before slow is non-zero. Everything after fast has not been examined yet.
Time: O(n). Space: O(1).
Two pointers: slow writes, fast scans
What Trips People Up in Real Interviews
Using extra space by creating a new array. The problem explicitly requires in-place modification.
Shifting all elements right when a zero is found. That is O(n^2). Use two pointers for O(n).
Not preserving relative order of non-zero elements. The relative order must stay the same, so you cannot just swap zeroes to the end arbitrarily.
Forgetting that the slow pointer should only advance when a non-zero swap occurs. If you advance it unconditionally, you overwrite elements.
Confusing this with removing zeroes entirely. The array length stays the same, zeroes just move to the end.
Solution Code
O(n) timedef moveZeroes(nums):
slow = 0
for fast in range(len(nums)):
if nums[fast] != 0:
nums[slow], nums[fast] = nums[fast], nums[slow]
slow += 1Frequently Asked Questions
What is the Move Zeroes problem?
Given an integer array nums, move all 0s to the end while maintaining the relative order of the non-zero elements. You must do this in-place without making a copy of the array.
How do you solve Move Zeroes?
The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Move Zeroes?
Move Zeroes is asked at Meta, Microsoft, Apple, Walmart. It is a easy difficulty problem.
What are common mistakes on Move Zeroes?
- Using extra space by creating a new array. The problem explicitly requires in-place modification.
- Shifting all elements right when a zero is found. That is `O(n^2)`. Use two pointers for `O(n)`.
- Not preserving relative order of non-zero elements. The relative order must stay the same, so you cannot just swap zeroes to the end arbitrarily.
- Forgetting that the slow pointer should only advance when a non-zero swap occurs. If you advance it unconditionally, you overwrite elements.
- Confusing this with removing zeroes entirely. The array length stays the same, zeroes just move to the end.