PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question tests a candidate's ability to perform in-place array manipulation while preserving element order, evaluating practical understanding of the two-pointer technique and space complexity constraints. It falls under array and algorithm fundamentals, commonly asked to assess whether engineers can operate within O(1) extra space and O(n) time bounds on partition-style problems.

  • medium
  • J.P. Morgan
  • Coding & Algorithms
  • Software Engineer

Shift Non-Zero Elements Left In Place

Company: J.P. Morgan

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Shift Non-Zero Elements Left In Place You are given an integer array `nums`. Rearrange the array **in place** so that every element equal to `0` is moved to the end of the array, while the **relative order** of all non-zero elements is preserved. You must modify `nums` directly. Do not allocate a separate output array. ### Example 1 ``` Input: nums = [0, 1, 0, 3, 12] Output: [1, 3, 12, 0, 0] ``` The non-zero elements `1`, `3`, `12` keep their original order, and the two zeros are pushed to the back. ### Example 2 ``` Input: nums = [0] Output: [0] ``` ### Example 3 ``` Input: nums = [5, 0, 0, 7, 0, 11] Output: [5, 7, 11, 0, 0, 0] ``` ### Constraints - `1 <= nums.length <= 10^5` - `-2^31 <= nums[i] <= 2^31 - 1` - You must operate in place with `O(1)` extra space. - Aim for a single pass (or at most two passes) over the array, i.e. `O(n)` time. ### Required Output Return the same array `nums` after rearranging it in place (the array is mutated; returning a reference to it is acceptable).

Quick Answer: This question tests a candidate's ability to perform in-place array manipulation while preserving element order, evaluating practical understanding of the two-pointer technique and space complexity constraints. It falls under array and algorithm fundamentals, commonly asked to assess whether engineers can operate within O(1) extra space and O(n) time bounds on partition-style problems.

You are given an integer array `nums`. Rearrange the array **in place** so that every element equal to `0` is moved to the end of the array, while the **relative order** of all non-zero elements is preserved. You must modify `nums` directly. Do not allocate a separate output array. ### Example 1 ``` Input: nums = [0, 1, 0, 3, 12] Output: [1, 3, 12, 0, 0] ``` The non-zero elements `1`, `3`, `12` keep their original order, and the two zeros are pushed to the back. ### Example 2 ``` Input: nums = [0] Output: [0] ``` ### Example 3 ``` Input: nums = [5, 0, 0, 7, 0, 11] Output: [5, 7, 11, 0, 0, 0] ``` ### Constraints - `1 <= nums.length <= 10^5` - `-2^31 <= nums[i] <= 2^31 - 1` - Operate in place with `O(1)` extra space. - Aim for a single pass (or at most two passes) over the array, i.e. `O(n)` time. ### Required Output Return the same array `nums` after rearranging it in place (the array is mutated; returning a reference to it is acceptable).

Constraints

  • 1 <= nums.length <= 10^5
  • -2^31 <= nums[i] <= 2^31 - 1
  • Must operate in place with O(1) extra space
  • Target O(n) time, at most two passes

Examples

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

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

Explanation: Non-zero elements 1, 3, 12 keep their order; the two zeros move to the end.

Input: ([0],)

Expected Output: [0]

Explanation: Single zero stays in place.

Input: ([5, 0, 0, 7, 0, 11],)

Expected Output: [5, 7, 11, 0, 0, 0]

Explanation: Non-zeros 5, 7, 11 preserved; three zeros pushed to the back.

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

Expected Output: [1, 2, 3]

Explanation: No zeros, so the array is unchanged.

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

Expected Output: [0, 0, 0]

Explanation: All zeros; the array remains the same.

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

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

Explanation: Negative non-zero values are handled the same way; -1, -2, 3 keep order and zeros go to the end.

Input: ([42],)

Expected Output: [42]

Explanation: Single non-zero element is unchanged.

Hints

  1. Maintain a write pointer that tracks the position where the next non-zero element should go.
  2. Walk through the array with a read pointer; whenever you hit a non-zero value, swap it into the write-pointer slot and advance the write pointer.
  3. Swapping (rather than overwriting) automatically pushes the zeros toward the back while keeping the non-zero order intact, and it keeps everything in O(1) extra space.
Last updated: Jun 24, 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
  • 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

  • Can All Courses Be Completed? - J.P. Morgan (medium)
  • Merge Overlapping Intervals - J.P. Morgan (medium)
  • Implement Integer Square Root - J.P. Morgan (medium)
  • Implement KNN from Scratch - J.P. Morgan (medium)
  • Minimize Operations to Balance Shipments - J.P. Morgan (medium)