Skip to main content

283. Move Zeroes

easyAsked at Goldman Sachs

Move all zeros to the end of an array while keeping non-zero element order, in-place. Goldman Sachs uses Move Zeroes to test the two-pointer in-place pattern — the candidate who avoids the extra-array detour gets the credit.

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Goldman Sachs loops.

  • Glassdoor (2026-Q1)Goldman Sachs SWE candidate reports list Move Zeroes as a two-pointer fluency check.
  • LeetCode Discuss (2025-11)Move Zeroes is in the top-30 of LeetCode's Goldman Sachs company tag.

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

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-pass: pack non-zeros, then fill zeros

First pass copies non-zero elements to the front. Second pass fills the tail with zeros.

Time
O(n)
Space
O(1)
function moveZeroesTwoPass(nums) {
  let writeIdx = 0;
  for (let i = 0; i < nums.length; i++) {
    if (nums[i] !== 0) nums[writeIdx++] = nums[i];
  }
  while (writeIdx < nums.length) nums[writeIdx++] = 0;
}

Tradeoff: Linear time, O(1) space, two passes. Clean and easy to reason about. Goldman accepts this as the baseline.

2. One-pass with swap (optimal)

Walk with a left pointer indicating the next position for a non-zero. Swap left and i when nums[i] is non-zero.

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

Tradeoff: Single pass with fewer total writes (each element is written at most twice, the two-pass does it twice in the worst case). The swap pattern is what Goldman wants — it generalizes to partition-like problems.

Goldman Sachs-specific tips

Goldman Sachs interviewers grade Move Zeroes on three axes: (1) in-place — no auxiliary array allowed, (2) the two-pointer pattern is named, (3) write count is minimized. The two-pass solution is acceptable; the one-pass swap version is preferred. Mention the write-count tradeoff out loud — that level of detail signals you think about cache and memory bus, which Goldman cares about for low-latency systems.

Common mistakes

  • Allocating a temporary array — the problem explicitly forbids it.
  • Sorting with a comparator that pushes zeros to the end — that's O(n log n) AND breaks 'maintain relative order'.
  • Using two pointers but moving the left pointer even when nums[i] is zero — silently shifts non-zero values into the wrong slot.

Follow-up questions

An interviewer at Goldman Sachs may pivot to one of these next:

  • Move all instances of value k to the end. (Same pattern, parameterize the predicate.)
  • Partition: rearrange so all evens come before all odds, maintaining relative order. (Stable partition — harder!)
  • Remove element in-place (LC #27) — same two-pointer template.

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 is the one-pass version preferred?

Because it does fewer writes when the array is mostly non-zero. The two-pass version unconditionally writes every position; the one-pass swap only writes when it sees a non-zero, AND only writes twice per non-zero. For arrays with very few zeros, that's a meaningful constant-factor win.

Is the swap version stable?

Yes — each non-zero element retains its relative order to other non-zeros because we only swap with the leftmost unfilled slot. Stable partitioning in O(1) extra space is a non-trivial property worth naming when Goldman asks.

Free learning resources

Curated free links for this problem.

Companies that also ask Move Zeroes