Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the remaining elements to the right.
Note that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.
Example 1:
Input: arr = [1,0,2,3,0,4,5,0] Output: [1,0,0,2,3,0,0,4] Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]
Example 2:
Input: arr = [1,2,3] Output: [1,2,3] Explanation: After calling your function, the input array is modified to: [1,2,3]
Constraints:
1 <= arr.length <= 1040 <= arr[i] <= 9Problem summary: Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the remaining elements to the right. Note that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers
[1,0,2,3,0,4,5,0]
[1,2,3]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1089: Duplicate Zeros
class Solution {
public void duplicateZeros(int[] arr) {
int n = arr.length;
int i = -1, k = 0;
while (k < n) {
++i;
k += arr[i] > 0 ? 1 : 2;
}
int j = n - 1;
if (k == n + 1) {
arr[j--] = 0;
--i;
}
while (j >= 0) {
arr[j] = arr[i];
if (arr[i] == 0) {
arr[--j] = arr[i];
}
--i;
--j;
}
}
}
// Accepted solution for LeetCode #1089: Duplicate Zeros
func duplicateZeros(arr []int) {
n := len(arr)
i, k := -1, 0
for k < n {
i, k = i+1, k+1
if arr[i] == 0 {
k++
}
}
j := n - 1
if k == n+1 {
arr[j] = 0
i, j = i-1, j-1
}
for j >= 0 {
arr[j] = arr[i]
if arr[i] == 0 {
j--
arr[j] = arr[i]
}
i, j = i-1, j-1
}
}
# Accepted solution for LeetCode #1089: Duplicate Zeros
class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
n = len(arr)
i, k = -1, 0
while k < n:
i += 1
k += 1 if arr[i] else 2
j = n - 1
if k == n + 1:
arr[j] = 0
i, j = i - 1, j - 1
while ~j:
if arr[i] == 0:
arr[j] = arr[j - 1] = arr[i]
j -= 1
else:
arr[j] = arr[i]
i, j = i - 1, j - 1
// Accepted solution for LeetCode #1089: Duplicate Zeros
impl Solution {
pub fn duplicate_zeros(arr: &mut Vec<i32>) {
let n = arr.len();
let mut i = 0;
let mut j = 0;
while j < n {
if arr[i] == 0 {
j += 1;
}
j += 1;
i += 1;
}
while i > 0 {
if arr[i - 1] == 0 {
if j <= n {
arr[j - 1] = arr[i - 1];
}
j -= 1;
}
arr[j - 1] = arr[i - 1];
i -= 1;
j -= 1;
}
}
}
// Accepted solution for LeetCode #1089: Duplicate Zeros
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1089: Duplicate Zeros
// class Solution {
// public void duplicateZeros(int[] arr) {
// int n = arr.length;
// int i = -1, k = 0;
// while (k < n) {
// ++i;
// k += arr[i] > 0 ? 1 : 2;
// }
// int j = n - 1;
// if (k == n + 1) {
// arr[j--] = 0;
// --i;
// }
// while (j >= 0) {
// arr[j] = arr[i];
// if (arr[i] == 0) {
// arr[--j] = arr[i];
// }
// --i;
// --j;
// }
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.