FrontendInterviews.dev

Loading problem…

66. Move Zeroes - Array Reordering

Easy•
Acceptance: 79.31%
•
🔓3/3 Pro unlocks today

You're building a data filtering feature that needs to move all zero values to the end of an array while maintaining the relative order of non-zero elements. This is common in data processing and UI filtering.

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.

Requirements

1. Basic Functionality

  • Move all zeros to the end of array
  • Maintain relative order of non-zero elements
  • Modify array in-place (O(1) extra space)
  • Handle edge cases (no zeros, all zeros, empty array)

Example Usage

// Example 1
let nums = [0,1,0,3,12];
moveZeroes(nums);
// nums becomes [1,3,12,0,0]

// Example 2
let nums = [0];
moveZeroes(nums);
// nums becomes [0]

Real-World Context

This problem models real frontend scenarios:

  • Data filtering: Move invalid/null values to end
  • Array cleanup: Reorder array elements efficiently
  • UI data processing: Filter and reorder list items
  • Form validation: Move invalid entries to end

Constraints

  • 1 <= nums.length <= 10^4
  • -2^31 <= nums[i] <= 2^31 - 1
  • Must modify array in-place with O(1) extra space