Merge Sorted Arrays in Place
Updated
Problem statement
The first m entries of nums1 and all n entries of nums2 are sorted. The first array has m+n slots, with its remaining entries serving only as unused capacity.
Merge both valid sequences into nums1 in nondecreasing order, in place. Ignore the original capacity values. Return the mutated first array as the observable result. The final approach should use constant auxiliary space.
Function
mergeSortedArray(nums1: int[], m: int, nums2: int[], n: int) → int[]Examples
Example 1
nums1 = [1,2,3,0,0,0]m = 3nums2 = [2,5,6]n = 3return = [1,2,2,3,5,6]The valid portions are [1,2,3] and [2,5,6]. Their merged order is [1,2,2,3,5,6].
Example 2
nums1 = [1]m = 1nums2 = []n = 0return = [1]The second array is empty, so nums1 remains unchanged.
Example 3
nums1 = [0]m = 0nums2 = [1]n = 1return = [1]nums1 has no valid input elements. Its single position is merge capacity for the value from nums2.
Constraints
0 <= m, n <= 2000.1 <= m+n <= 2000.-1000000000 <= nums1[i], nums2[i] <= 1000000000.- nums1 has m+n entries. nums2 has n entries. The first m entries of nums1 and all entries of nums2 are nondecreasing.
More Expedia Phone Screen questions
- Find the Shortest Round TripPHONE SCREEN · Seen Jul 2026
- Two SumPHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2024
- Merge Two Sorted Linked ListsPHONE SCREEN · ONSITE INTERVIEW · Seen May 2023
- Assign CookiesPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
- Kth Smallest Value in a BSTPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2022
- Reverse a Singly Linked ListPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2022
- Climbing StairsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2021
- First Substring OccurrencePHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2021
- Array Containment with MultiplicitiesPHONE SCREEN · Seen Nov 2019
- Subtract the Next Smaller ValuePHONE SCREEN · Seen Oct 2018
Similar questions at other companies
- Merge Sorted ArrayDP World · ONSITE INTERVIEW
- Squares of a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
- Merge Two Sorted ArraysCapgemini · PHONE SCREENCapgemini interview questions
- Merge Two Arrays Into Sorted OrderNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Two SumMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Merge Sorted ArrayAmazon · ONSITE INTERVIEW
- 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
- Sorted Squares in Descending OrderWhatNot · PHONE SCREENWhatNot interview questions