L2 · Working engineerLists & arrays~8 min · 6 tests#6

Move All Zeros to the End

Move every zero to the end of a Python list in place while keeping the order of non-zero values. Two-pointer O(n) time, O(1) space.

The problem

Move every 0 to the end of nums in place, keeping the relative order of the non-zero values. Return nums.

Try it without building a second list.

Examples

  1. Example 1

    Input

    move_zeros([0, 1, 0, 3, 12, 0, 5])

    Expected output

    [1, 3, 12, 5, 0, 0, 0]
  2. Example 2

    Input

    move_zeros([0, 0, 1])

    Expected output

    [1, 0, 0]

+ 4 hidden tests on Submit — no zeros, all zeros, mutates in place.

Edge cases to ask about

  • No zeros
  • All zeros
  • Zeros already at the end

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · move_zeros
    ⌘/Ctrl + Enter runs the examples

    Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.

    Complexity Lab

    What does this cost as n grows?

    Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    Measure it

    Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    Filter into a new listO(n)O(n)Collect non-zeros, pad with zeros, copy back. Simple but allocates.
    bestRead/write pointersO(n)O(1)write marks where the next non-zero goes; swapping keeps it in place.
    Walkthrough of the optimal approach (try it yourself first)

    Two pointers on the same list: read visits every index, write points at the slot where the next non-zero belongs. Each time read finds a non-zero, swap it into write and move write forward. Everything after write ends up as zeros, and non-zero order is preserved because they are placed in the order they were read.

    Complexity: O(n) time, O(1) space. Each index is read once; the swaps happen inside the original list, so extra memory is constant.

    Reveal the reference solution
    def move_zeros(nums):
        write = 0
        for read in range(len(nums)):
            if nums[read] != 0:
                nums[write], nums[read] = nums[read], nums[write]
                write += 1
        return nums

    The brute force, for comparison

    def move_zeros(nums):
        non_zero = [x for x in nums if x != 0]
        nums[:] = non_zero + [0] * (len(nums) - len(non_zero))
        return nums

    Follow-ups interviewers ask

    • Minimise the number of writes.
    • Move all occurrences of any value k, not just 0.

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of Move All Zeros to the End in Python?

    The optimal solution runs in O(n) time and O(1) auxiliary space. Each index is read once; the swaps happen inside the original list, so extra memory is constant.

    What is the brute-force approach, and how do you optimise it?

    Filter into a new list: O(n) time, O(n) space. Collect non-zeros, pad with zeros, copy back. Simple but allocates. Read/write pointers: O(n) time, O(1) space. `write` marks where the next non-zero goes; swapping keeps it in place.

    What follow-up questions do interviewers ask about Move All Zeros to the End?

    Minimise the number of writes. Move all occurrences of any value k, not just 0.