Duplicate Zeros Within a Fixed Array

Quick Overview

Duplicate Zeros Within a Fixed Array. Modify arr in place by duplicating each original zero and shifting following elements right.

Duplicate Zeros Within a Fixed Array

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Modify arr in place by duplicating each original zero and shifting following elements right. Keep the original array length: discard everything that would fall beyond its last position. The native method returns nothing; the result is the mutated input array. ```cpp class Solution { public: void duplicateZeros(vector<int>& arr); }; ``` ### Examples **Example 1:** ```text Input: arr = [0,2,0,3,4] Output: arr after the call = [0,0,2,0,0] Explanation: The expanded sequence begins0,0,2,0,0; later values do not fit. ``` **Example 2:** ```text Input: arr = [5,0] Output: arr after the call = [5,0] Explanation: The second copy of the last zero is outside the fixed-length array. ``` ### Constraints - The array has between 1 and 10000 elements. - Every entry is an integer digit in `[0,9]`. ### Contract considerations Preserve `void duplicateZeros(vector<int>& arr)` and its observable in-place mutation. The examples describe array state, not a returned array. ```hint Read the boundary condition If duplicating a zero would pass the original final index, which array positions may the method actually change? ``` ### Boundary reminders Preserve `void duplicateZeros(vector<int>& arr)` and its observable in-place mutation. The examples describe array state, not a returned array.

Overview: Duplicate Zeros Within a Fixed Array. Modify arr in place by duplicating each original zero and shifting following elements right.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Modify arr in place by duplicating each original zero and shifting following elements right. Keep the original array length: discard everything that would fall beyond its last position. The native method returns nothing; the result is the mutated input array.

class Solution {
public:
    void duplicateZeros(vector<int>& arr);
};

Examples

Example 1:

Input: arr = [0,2,0,3,4]
Output: arr after the call = [0,0,2,0,0]
Explanation: The expanded sequence begins0,0,2,0,0; later values do not fit.

Example 2:

Input: arr = [5,0]
Output: arr after the call = [5,0]
Explanation: The second copy of the last zero is outside the fixed-length array.

Constraints

  • The array has between 1 and 10000 elements.
  • Every entry is an integer digit in [0,9] .

Contract considerations

Preserve void duplicateZeros(vector<int>& arr) and its observable in-place mutation. The examples describe array state, not a returned array.

Boundary reminders

Preserve void duplicateZeros(vector<int>& arr) and its observable in-place mutation. The examples describe array state, not a returned array.