Skip to main content

283. Move Zeroes

easyAsked at Cohere

Move all zeroes to the end of an array while preserving the relative order of non-zero elements. Cohere asks this to test in-place two-pointer discipline — the same pattern used when compacting sparse token arrays in a batched inference engine.

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

Source citations

Public interview reports confirming this problem appears in Cohere loops.

  • Glassdoor (2025-Q3)Reported by Cohere SWE candidates as a two-pointer warm-up in early rounds.
  • Blind (2025-07)Cohere prep threads mention Move Zeroes as a good pointer-practice problem before harder in-place questions.

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]

Explanation: Non-zeros 1, 3, 12 maintain order; zeroes move to end.

Example 2

Input
nums = [0]
Output
[0]

Explanation: Single zero — already valid.

Approaches

1. Two-pointer (write pointer)

Use a write pointer that tracks where the next non-zero should go. Walk the read pointer; whenever a non-zero is found, write it at the write position. Then fill the remainder with zeroes.

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

Tradeoff: O(n) time, O(1) space. Two passes: one to compact non-zeroes, one to fill zeroes. Minimises unnecessary writes compared to the swap approach when arrays are dense.

2. Swap on non-zero

Swap each non-zero element with the element at the write pointer position. This avoids the second fill pass but does more swaps when zeros are sparse.

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

Tradeoff: Single pass, but swaps may be wasteful when most elements are non-zero (many self-swaps). Discuss the trade-off with the interviewer.

Cohere-specific tips

Cohere may ask about minimising write operations — relevant to flash-memory or cache-line-optimised buffers. The write-pointer approach with a separate fill pass writes each position at most twice (once with data, once with zero), while the swap approach may perform unnecessary swaps on already-correct positions. Articulating this trade-off shows systems awareness beyond the algorithm itself.

Common mistakes

  • Making a copy of the array — the problem explicitly requires in-place modification.
  • Not preserving relative order — sorting the array moves zeroes to the front or back but breaks order.
  • Using splice or filter — O(n) auxiliary space under the hood.
  • Swapping even when write === read — unnecessary; add a guard if optimising for write count.

Follow-up questions

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

  • Remove Element (LC 27) — same write-pointer pattern for a target value.
  • Remove Duplicates from Sorted Array — write-pointer with a uniqueness check.
  • How would you compact sparse rows in a matrix without extra space?

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Does the swap approach preserve relative order?

Yes — because the write pointer only advances past non-zero elements in their original order, swaps push zeroes right without disturbing non-zero relative positions.

Which approach is better?

The write-then-fill approach minimises writes in zero-dense arrays. The swap approach is a single pass. Both are O(n) — choose based on the interviewer's constraints.

What if the input has no zeroes?

Both approaches handle this cleanly: the write pointer reaches nums.length with no fill needed, and the swap approach performs only self-swaps (which a guard can eliminate).

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →