Duplicate Zeros
Updated
Problem statement
Given a fixed-length integer array arr, duplicate each occurrence of 0. Shift the remaining values to the right and discard any values that move beyond the original array length.
Modify the array in place. Return the same array after you have changed it.
Function
duplicateZeros(arr: int[]) → int[]Examples
Example 1
arr = [1,0,2,3,0,4,5,0]return = [1,0,0,2,3,0,0,4]Each zero is copied once. Values shifted beyond the eighth position are discarded.
Example 2
arr = [1,2,3]return = [1,2,3]There are no zeros to duplicate, so the array is unchanged.
Constraints
1 <= arr.length <= 1000000 <= arr[i] <= 10^9
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Sparse Vector Dot Product from TuplesMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Merge Sorted ArrayDP World · ONSITE INTERVIEW
- Remove Duplicates From Sorted Array In PlaceKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Maximize Distance to the Closest Occupied SeatAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Merge Two Arrays Into Sorted OrderNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Alternate Positive and Negative ValuesTesla · ONSITE INTERVIEWTesla interview questions
- Make a Sorted Array Unique In PlaceAlpaca · ONSITE INTERVIEW
- Maximize Distance to Closest Person — Return the SeatRoblox · ONSITE INTERVIEWRoblox interview questions
- Move ZeroesBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Rotate an Array to the RightScale AI · ONSITE INTERVIEWScale AI interview questions