PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates array manipulation skills, in-place algorithm design, and reasoning about time and space complexity while preserving element order under an O(1) extra space constraint.

  • medium
  • Uber
  • Coding & Algorithms
  • Data Scientist

Move zeros to the front

Company: Uber

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given an integer array nums, move all elements equal to 0 to the beginning of the array while preserving the relative order of all non-zero elements. The transformation must be done in-place using O(1) extra space. Example: - Input: [1, 0, 2, 0, 3] - Output: [0, 0, 1, 2, 3] Discuss the time complexity you would target.

Quick Answer: This question evaluates array manipulation skills, in-place algorithm design, and reasoning about time and space complexity while preserving element order under an O(1) extra space constraint.

Given an integer array nums, move all elements equal to 0 to the beginning of the array while preserving the relative order of all non-zero elements. The transformation must be done in-place using O(1) extra space. For example, if nums = [1, 0, 2, 0, 3], the result should be [0, 0, 1, 2, 3]. Aim for O(n) time complexity. For evaluation, return the modified array after performing the in-place transformation.

Constraints

  • 0 <= len(nums) <= 100000
  • -1000000000 <= nums[i] <= 1000000000
  • The operation must use O(1) extra space

Examples

Input: [1, 0, 2, 0, 3]

Expected Output: [0, 0, 1, 2, 3]

Explanation: The two zeros move to the front, and the non-zero values 1, 2, 3 keep their original order.

Input: [0, 0, 1, 2]

Expected Output: [0, 0, 1, 2]

Explanation: Zeros are already at the front, so the array stays the same.

Input: [1, 2, 3]

Expected Output: [1, 2, 3]

Explanation: There are no zeros to move.

Input: [0, 0, 0]

Expected Output: [0, 0, 0]

Explanation: All elements are zero, so the array remains unchanged.

Input: []

Expected Output: []

Explanation: An empty array should return an empty array.

Input: [-1, 0, -2, 0, 3]

Expected Output: [0, 0, -1, -2, 3]

Explanation: Zeros move to the front, and the non-zero values -1, -2, 3 keep their relative order.

Hints

  1. Try processing the array from right to left and placing non-zero values at the end.
  2. Once all non-zero values are in their final relative order, fill the remaining positions at the front with 0.
Last updated: May 6, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Maximize Throughput and Count Trigger Components - Uber (medium)
  • Replace Dashes With Nearest Letters - Uber (medium)
  • Find Earliest Column With One - Uber (easy)
  • Solve Wonderful Strings and Grid Queries - Uber (hard)
  • Count Islands After Land Additions - Uber (medium)