Question bankPricingSign in

Move Zeroes

Two PointersMedium1:30

Given an integer array, move all zeros 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.

For example, given nums = [0, 1, 0, 3, 12], the output is [1, 3, 12, 0, 0].

Explain your approach, why it preserves relative order, and analyze the time and space complexity. Discuss how you would minimize the number of operations.

How to approach it

  • Hint 1

    Think about partitioning the array: non-zero elements go to the front, zeros go to the back.

  • Hint 2

    Use a slow pointer to track where the next non-zero element should be placed.

  • Hint 3

    The slow pointer marks the boundary between processed non-zero elements and the rest of the array.

Ready to answer it out loud?

Record your answer in 1:30 and Preptile scores it 1–10 with specifics — what landed, what you skipped, and what to say next time.

Practising needs an invite code. Join the waitlist and we’ll send you one.