Move All Zeros to the End

This one looks simple. Just move the zeros to the end. But the interviewer is not really asking about zeros. They want to see if you can change an array in place. In place means you use the same array, without making a new one. They also watch one more thing closely. Do the non-zero numbers stay in the same order? That order is the real test. So let us talk through it the way you would in a real interview.

πŸ”€ The Problem

You get an array of numbers. Some of them are zeros. You have to push all the zeros to the end. But the non-zero numbers must keep their original order. And you should do this without creating a second array.

Input: [0, 1, 0, 3, 12]
Output: [1, 3, 12, 0, 0]

See how 1, 3, and 12 are still in the same order they came in? Only the zeros got pushed to the back. That is the whole job.

🐒 The Simple Approach First

The first idea that comes to most people is the easy one. Make a brand new array. Walk through the old array. Every time you find a non-zero number, put it in the new array. Then fill whatever space is left with zeros.

This works, and it gives the right answer. But there is a problem. You made a second array, so you are using extra memory. The size of that extra memory grows with the input. If the interviewer says β€œdo it in place”, this approach fails that rule right away. So it is fine as a first thought, but we can do better.

⚑ The Optimal Approach

Here is the clean idea. We use one pointer, and we call it the write index. The write index marks the next spot where a non-zero number should go.

Now walk through the array once with a normal loop. Every time you see a non-zero number, write it at the write index, then move the write index forward by one. When the loop finishes, every non-zero number is sitting at the front in the correct order. The write index now points to the first leftover spot. So just fill everything from there to the end with zeros. Done.

We never made a new array. We used the same one. That is the O(1) space the interviewer wants. It means we used a fixed, tiny amount of extra memory no matter how big the array is.

Steps to Move Zeros to the End

  1. Set a writeIndex to 0. This is where the next non-zero number will go.
  2. Loop through the array from start to end with a reading index.
  3. If the current number is not zero, copy it to position writeIndex, then add one to writeIndex.
  4. After the loop, all non-zero numbers are at the front in order.
  5. Fill the rest of the array, from writeIndex to the end, with zeros.
move_zeros.py
# Move all zeros to the end, keep order, in place
def move_zeros(arr):
write_index = 0
# Place every non-zero number at the front
for i in range(len(arr)):
if arr[i] != 0:
arr[write_index] = arr[i]
write_index += 1
# Fill the rest with zeros
while write_index < len(arr):
arr[write_index] = 0
write_index += 1
arr = [0, 1, 0, 3, 12]
move_zeros(arr)
print(arr)

The output of the above code will be:

[1, 3, 12, 0, 0]

⏱️ Time and Space Complexity

Let us compare the two approaches side by side. Here n is the number of items in the array.

Approach Time Space
New array (simple) O(n) O(n)
Write index (optimal) O(n) O(1)

Both walk through the array once, so both are O(n) in time. The difference is the memory. The simple way needs a whole second array. The write index way needs just one extra variable. That is why the second one wins.

Tip

In the interview, say out loud that the non-zero numbers must keep their order. That single sentence shows the interviewer you understood the real requirement. Many candidates miss it and accidentally use a swap that reorders the numbers.

🧩 Key Takeaways

  • βœ… The real test is doing it in place, using the same array with O(1) extra space.
  • βœ… Use a write index to mark the next spot for a non-zero number.
  • βœ… First pass copies all non-zero numbers to the front in their original order.
  • βœ… Then fill the leftover spots with zeros, all the way to the end.
  • βœ… Both approaches are O(n) in time, so the only thing that separates them is memory.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What is the interviewer mainly testing with this problem?

    Why: The key requirements are doing it in place with O(1) space and keeping the original order of the non-zero numbers.

  2. 2

    What does the write index point to during the first pass?

    Why: The write index always marks the next position to place a non-zero number.

  3. 3

    Why is the simple new-array approach not ideal?

    Why: It works correctly but needs a second array, which is O(n) extra space instead of O(1).

  4. 4

    After the first pass copies all non-zero numbers to the front, what do you do next?

    Why: Once non-zeros are in place, every spot from the write index onward is filled with zeros.

πŸš€ What’s Next?

Share & Connect