Merge Two Sorted Arrays
Updated
Problem statement
You get two integer arrays, nums1 and nums2. Within each array the values never go down from left to right (non-decreasing order): every value is less than or equal to the value after it.
Build a single array in non-decreasing order that holds every value of both arrays. A value keeps every copy it has in the two arrays, so the returned array has nums1.length + nums2.length values.
One or both arrays can have length 0.
Function
mergeSortedArrays(nums1: int[], nums2: int[]) → int[]Examples
Example 1
nums1 = [2,5]nums2 = [1,5]return = [1,2,5,5]The smallest value overall is 1, from nums2; next is 2, from nums1. Each input holds one 5, so the answer ends with two copies of 5.
Example 2
nums1 = []nums2 = [4,6]return = [4,6]With nums1 empty, the returned array equals nums2.
Example 3
nums1 = [-3,0,0,8]nums2 = [-5,0,9]return = [-5,-3,0,0,0,8,9]The three copies of 0 all stay, two taken from nums1 plus one taken from nums2; the returned array has 4 + 3 = 7 values.
Constraints
0 <= nums1.length, nums2.length <= 105-109 <= nums1[i], nums2[j] <= 109nums1andnums2are each sorted in non-decreasing order.
More Zillow Phone Screen questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Time Based Key-Value StorePHONE SCREEN · Seen Feb 2024
- Filter Formatted Records by CriteriaPHONE SCREEN · Seen Sep 2023
- Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- Jump GamePHONE SCREEN · Seen Dec 2022
- Reversible Run-Length EncodingPHONE SCREEN · Seen Nov 2022
- Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
- Two Sum: All PairsPHONE SCREEN · Seen Nov 2022
- Employee Free TimePHONE SCREEN · Seen Oct 2022
- First Unique Character in a StringPHONE SCREEN · Seen Oct 2022
Similar questions at other companies
- Squares of a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
- Assign CookiesAmazon · PHONE SCREEN
- Merge Two Sorted ArraysCapgemini · PHONE SCREENCapgemini interview questions
- Squares of a Sorted ArrayStryker · PHONE SCREEN
- Squares of a Sorted ArrayGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Merge Into the Zero-Padded ArrayAdobe · PHONE SCREENAdobe interview questions
- Three Partition ArrayMicrosoft · PHONE SCREENMicrosoft interview questions
- Sorted Squares in Descending OrderWhatNot · PHONE SCREENWhatNot interview questions
- Assign CookiesExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions
- Merge Sorted Arrays with DuplicatesMygate · PHONE SCREENMygate interview questions