EasyArrayTwo PointersUpdated Sep 2026

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

nums01031201234Start: the two zeros (red) are mixed in with thenon-zero values.nums13120001234End: non-zeros keep their order, and both zeros(green) are at the end.
The non-zero values 1, 3, 12 must stay in that order, and the zeros must end up at the right side.

Constraints

  • ●1 <= nums.length <= 10^4
  • ●-2^31 <= nums[i] <= 2^31 - 1

Examples

Example 1
Inputnums = [0,1,0,3,12]
Output[1,3,12,0,0]
WhyThe non-zero values 1, 3 and 12 keep their original order and move to the front. The two zeros are pushed to the back.
Example 2
Inputnums = [0]
Output[0]
WhyThere is only one element and it is a zero, so nothing changes.

Asked At

4 companies

How to Think About It

1

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.

2

Why swap instead of shift: shifting all elements right is O(n^2). Swapping two elements is O(1) per operation. Total: O(n).

3

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.

4

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

nums01031201234slowfastBoth pointers start at index 0. nums[fast] is 0, so weskip it.nums10031201234slowfastfast finds 1, so we swap it into slot slow. Now slowmoves to index 1.nums13120001234slowfastAfter the last swap, the three non-zeros sit at thefront and slow points past them.if nums[fast] != 0: swap nums[slow], nums[fast] slow += 1
slow marks the next slot for a non-zero. fast looks at every element once.
ComplexityTime O(n)Space O(1)We scan the array once with the fast pointer, and each swap is a constant-time operation, so the total work grows linearly with the array length.

What Trips People Up in Real Interviews

1

Using extra space by creating a new array. The problem explicitly requires in-place modification.

2

Shifting all elements right when a zero is found. That is O(n^2). Use two pointers for O(n).

3

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.

4

Forgetting that the slow pointer should only advance when a non-zero swap occurs. If you advance it unconditionally, you overwrite elements.

5

Confusing this with removing zeroes entirely. The array length stays the same, zeroes just move to the end.

Solution Code

O(n) time
def moveZeroes(nums):
    slow = 0
    for fast in range(len(nums)):
        if nums[fast] != 0:
            nums[slow], nums[fast] = nums[fast], nums[slow]
            slow += 1

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently 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.