PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates array manipulation and in-place algorithm competencies, including handling space-time trade-offs and preserving relative order (stability) while operating with O(1) extra space.

  • 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

You are given an integer array `nums`. Reorder the array in place so that all `0` values are moved to the beginning of the array, while preserving the relative order of all non-zero elements. Requirements: - The transformation must be done in place. - Use only `O(1)` extra space. - The relative order of non-zero elements must remain unchanged. Example: - Input: `[4, 0, 5, 0, 3]` - Output: `[0, 0, 4, 5, 3]`

Quick Answer: This question evaluates array manipulation and in-place algorithm competencies, including handling space-time trade-offs and preserving relative order (stability) while operating with O(1) extra space.

Move all zeros to the beginning while preserving the relative order of non-zero values.

Examples

Input: ([4, 0, 5, 0, 3],)

Expected Output: [0, 0, 4, 5, 3]

Explanation: Prompt example.

Input: ([0, 0, 1],)

Expected Output: [0, 0, 1]

Explanation: Zeros already at front.

Input: ([1, 2, 3],)

Expected Output: [1, 2, 3]

Explanation: No zeros.

Input: ([],)

Expected Output: []

Explanation: Empty array.

Hints

  1. Fill non-zero values from right to left, then fill the prefix with zeros.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ 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
  • AI Coding 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

  • Deep Equality of Two Records - Uber (medium)
  • Shortest Path in a Grid with Blocked Cells - Uber (medium)
  • Design and Implement an LRU Cache - Uber (medium)
  • Reconstruct the Alphabet Order of an Alien Language - Uber (medium)
  • Maximize Throughput and Count Trigger Components - Uber (medium)