Duplicate Zeros — Solution in Python, Java & C++
Duplicate Zeros solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.
Duplicate Zeros is an easy coding problem on arrays and two pointers, asked in Amazon and Bloomberg interviews.
- Difficulty: Easy
- Topics: Arrays, Two Pointers
- Asked at: Amazon, Bloomberg
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given a fixed-length array arr, duplicate every occurrence of 0, shifting the remaining elements to the right.
Elements pushed past the end of the array are discarded — the length never changes. Return the resulting array.
Example 1
Input: arr = [1,0,2,3,0,4,5,0]
Output: [1,0,0,2,3,0,0,4]
Explanation: The trailing 5 and 0 are pushed off the end.
Example 2
Input: arr = [1,2,3]
Output: [1,2,3]
Explanation: No zeros, so nothing moves.
Example 3
Input: arr = [0,0,0]
Output: [0,0,0]
Constraints
1 <= arr.length <= 100000 <= arr[i] <= 9
Hints for Duplicate Zeros
Hint 1
Writing into the array left-to-right overwrites values you have not read yet.
Hint 2
The simplest correct approach builds the answer in a second buffer and stops once it is full.
Hint 3
For the O(1)-space version, first count how many zeros survive, then copy backwards from the end.
How to solve Duplicate Zeros
Every zero costs one extra slot. Build the output left to right and simply stop the moment it reaches the original length — everything after that would have fallen off the end anyway.
Approach
- Walk the input left to right, appending each element to an output buffer.
- After appending a
0, append a second0— but only if there is still room. - Stop as soon as the buffer holds
nelements and return it.
Why it works
The output is produced in the same order the problem describes, and truncation at n matches "elements beyond the length are not written". The room check before the duplicate is what prevents writing an n+1-th element.
Complexity
- Time —
O(n) - Space —
O(n) for the buffer (O(1) is possible with a backwards two-pointer copy)
Pitfalls
- Shifting the tail on every zero is O(n²) and times out on long arrays.
- Duplicating in place from the left destroys unread input.
- A zero in the very last slot must not be duplicated — the check has to happen before the second write.
Reference solution
Python
from typing import List
def duplicateZeros(arr: List[int]) -> List[int]:
n = len(arr)
out = []
for x in arr:
if len(out) >= n:
break
out.append(x)
if x == 0 and len(out) < n:
out.append(0)
return outJava
public static int[] duplicateZeros(int[] arr) {
int n = arr.length;
int[] out = new int[n];
int j = 0;
for (int i = 0; i < n && j < n; i++) {
out[j++] = arr[i];
if (arr[i] == 0 && j < n) out[j++] = 0;
}
return out;
}C++
vector<int> duplicateZeros(vector<int>& arr) {
int n = (int) arr.size();
vector<int> out;
for (int i = 0; i < n && (int) out.size() < n; i++) {
out.push_back(arr[i]);
if (arr[i] == 0 && (int) out.size() < n) out.push_back(0);
}
return out;
}JavaScript
var duplicateZeros = function(arr) {
const n = arr.length;
const out = [];
for (let i = 0; i < n && out.length < n; i++) {
out.push(arr[i]);
if (arr[i] === 0 && out.length < n) out.push(0);
}
return out;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Two Pointers Technique