Move Zeroes (LeetCode
Learn how to solve the Move Zeroes problem using Brute Force and the Optimal Two Pointer approach. Includes intuition, dry run, Java code, complexity analysis, interview tips, and production insights.
Introduction
Move Zeroes is one of the most popular Array + Two Pointer interview problems.
It is frequently asked by companies such as:
- Amazon
- Microsoft
- Meta
- Apple
- Adobe
- Uber
- Goldman Sachs
This problem evaluates your understanding of:
- Two Pointer Technique
- In-place array modification
- Array traversal
- Time and Space optimization
Although simple, interviewers often use it to test whether you can optimize from O(n²) to O(n).
Problem Statement
Given an integer array, move all 0's to the end of the array while maintaining the relative order of the non-zero elements.
Requirements
- Modify the array in-place.
- Do not create another array.
- Preserve the order of non-zero elements.
Example
Input
[0,1,0,3,12]
Output
[1,3,12,0,0]
Approach 1 — Brute Force
Idea
Create a temporary array.
- Copy all non-zero elements.
- Fill remaining positions with zero.
Illustration
Original Array
↓
Copy Non-Zero Values
↓
Fill Remaining With Zero
Java Code
public class MoveZeroesBruteForce {
public void moveZeroes(int[] nums) {
int[] temp = new int[nums.length];
int index = 0;
for (int num : nums) {
if (num != 0) {
temp[index++] = num;
}
}
for (int i = 0; i < nums.length; i++) {
nums[i] = temp[i];
}
}
}
Complexity
| Complexity | Value |
|---|---|
| Time | O(n) |
| Space | O(n) |
Although the time complexity is good, the extra space violates the in-place requirement.
Approach 2 — Optimal Two Pointer
Core Idea
Maintain a pointer that tracks the next position where a non-zero element should be placed.
When a non-zero element is found:
- Swap it with the element at the current insertion position.
- Move the insertion pointer forward.
Algorithm
Read Array
↓
Non-Zero?
↓
Swap
↓
Move Left Pointer
↓
Continue
Dry Run
Input
[0,1,0,3,12]
Initial
insertPosition = 0
Step 1
0
Ignore
Step 2
1
Swap
↓
[1,0,0,3,12]
insertPosition = 1
Step 3
0
Ignore
Step 4
3
Swap
↓
[1,3,0,0,12]
insertPosition = 2
Step 5
12
Swap
↓
[1,3,12,0,0]
Final Answer
[1,3,12,0,0]
Java Code
public class MoveZeroes {
public void moveZeroes(int[] nums) {
int insertPosition = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
int temp = nums[i];
nums[i] = nums[insertPosition];
nums[insertPosition] = temp;
insertPosition++;
}
}
}
}
Complexity
| Complexity | Value |
|---|---|
| Time | O(n) |
| Space | O(1) |
This is the optimal solution.
Why Does This Work?
The algorithm ensures:
- All non-zero elements move to the front.
- Their relative order remains unchanged.
- Zeroes naturally move toward the end through swapping.
Illustration
Original
0 1 0 3 12
↓
Swap Non-Zero
↓
1 3 12 0 0
Visual Explanation
Array
0 1 0 3 12
↓
Insert Position
0
↓
Swap 1
↓
1 0 0 3 12
↓
Swap 3
↓
1 3 0 0 12
↓
Swap 12
↓
1 3 12 0 0
Edge Cases
All Zeroes
[0,0,0]
Output
[0,0,0]
No Zeroes
[1,2,3]
Output
[1,2,3]
Single Element
[0]
Output
[0]
Empty Array
[]
Output
[]
Production Applications
The same in-place partitioning technique is widely used in enterprise software.
Examples
- Data cleansing pipelines
- Log processing
- Event filtering
- ETL transformations
- Inventory management
- Customer record cleanup
- Batch processing systems
- Stream processing applications
Common Interview Mistakes
❌ Creating another array when the problem asks for in-place modification.
❌ Not preserving the order of non-zero elements.
❌ Swapping every element unnecessarily.
❌ Using nested loops.
❌ Forgetting that swapping with the same index is safe.
Follow-Up Questions
Can this be solved without swapping?
Yes.
Overwrite non-zero elements first.
Then fill remaining positions with zero.
Time
O(n)
Space
O(1)
Is the solution stable?
Yes.
The relative order of non-zero elements is preserved.
Which technique is used?
The Two Pointer Technique.
One pointer scans the array.
The other tracks the insertion position.
Interview Tips
Interviewers commonly ask:
- Why use Two Pointers?
- Why is this in-place?
- Is the algorithm stable?
- Can it be solved without swapping?
- What is the time complexity?
- What is the space complexity?
- Why is only one traversal required?
- Explain the dry run.
Always explain the pointer movement before writing code.
Summary
The Move Zeroes problem is a classic Two Pointer interview question that demonstrates how in-place array manipulation can produce an efficient solution. By tracking the next insertion position for non-zero elements, we can rearrange the array in O(n) time and O(1) space while preserving the relative order of non-zero values.
Key Takeaways
- Understand the problem requirements.
- Start with the brute-force solution.
- Optimize using Two Pointers.
- Maintain the order of non-zero elements.
- Modify the array in-place.
- Achieve O(n) time complexity.
- Use O(1) extra space.
- Avoid unnecessary swaps.
- Practice dry runs for pointer movement.
- Relate the solution to real-world data processing scenarios.