Skip to main content

81. Move Zeroes

easyAsked at Workday

Move all zeros to the end of an array while preserving the order of non-zero elements. Workday uses this for two-pointer in-place fluency — same shape as deferring 'inactive' employee records to the end of an active-roster array.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Workday loops.

  • Glassdoor (2025)Workday SDE1 phone screen.

Problem

Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array.

Constraints

  • 1 <= nums.length <= 10^4
  • -2^31 <= nums[i] <= 2^31 - 1
  • Follow-up: Could you minimize the total number of operations done?

Examples

Example 1

Input
nums = [0,1,0,3,12]
Output
[1,3,12,0,0]

Example 2

Input
nums = [0]
Output
[0]

Approaches

1. Two passes (compact then fill)

Walk and copy non-zeros to front; fill rest with zeros.

Time
O(n)
Space
O(1)
let slow = 0;
for (let fast = 0; fast < nums.length; fast++) if (nums[fast] !== 0) nums[slow++] = nums[fast];
while (slow < nums.length) nums[slow++] = 0;

Tradeoff: Two passes, second is overwrite zeros. Total ops can be reduced.

2. Single-pass swap

Slow pointer = next non-zero slot. Fast scans; on non-zero, swap with slow and advance both.

Time
O(n)
Space
O(1)
function moveZeroes(nums) {
  let slow = 0;
  for (let fast = 0; fast < nums.length; fast++) {
    if (nums[fast] !== 0) {
      [nums[slow], nums[fast]] = [nums[fast], nums[slow]];
      slow++;
    }
  }
}

Tradeoff: Single pass, fewer writes (skips swapping zeros with themselves at the start).

Workday-specific tips

Workday wants the single-pass swap. State that this minimizes writes when many zeros are at the front (which is the follow-up). The swap is a no-op when slow == fast, so we don't write zero-for-zero.

Common mistakes

  • Two passes when one suffices.
  • Swapping when slow == fast — extra writes, no harm but slower for the follow-up.
  • Using filter + concat — allocates a new array.

Follow-up questions

An interviewer at Workday may pivot to one of these next:

  • Move Zeroes minimizing writes — guard the swap on slow != fast.
  • What if you want zeros at the front?
  • Remove Element (LC 27) — same shape.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why swap and not just copy?

Copy-and-fill requires two passes. Swap is single-pass and preserves the trailing zeros naturally as fast advances past them.

Minimize writes?

Add `if (slow !== fast)` before the swap. Skips no-op swaps for non-zeros that are already in place.

Practice these live with InterviewChamp.AI

Drill Move Zeroes and other Workday interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →