Merge Sorted Array
Updated
Problem statement
nums1 has length m+n. Its first m values and all n values of nums2 are sorted nondecreasingly; the remaining positions of nums1 are capacity.
Merge both sorted sequences into nums1 and return it.
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 two sorted prefixes merge in nondecreasing order.
Example 2
nums1 = [0]m = 0nums2 = [1]n = 1return = [1]The first sequence is empty.
Constraints
0 <= m, nand1 <= m+n <= 10^5.nums1.length == m+nandnums2.length == n.
More Motive Onsite Interview questions
- Group AnagramsPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2026
- Longest Equal-Zero-One SubarrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2026
- Maximum Weighted Sum After Left RotationsONSITE INTERVIEW · Seen Jan 2026
- Valid Parenthesis StringPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2026
- Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Dec 2025
- Target Triplet SumONSITE INTERVIEW · Seen Dec 2025
- Validate IP AddressONSITE INTERVIEW · Seen Dec 2025
- Generate Palindromic PermutationsONSITE INTERVIEW · Seen Dec 2025
- Minimum Swaps to Group Circular OnesONSITE INTERVIEW · Seen Dec 2025
- Staircase Search In A Sorted MatrixONSITE INTERVIEW · Seen Dec 2025
Similar questions at other companies
- 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
- Sparse Vector Dot Product from TuplesMeta · ONSITE INTERVIEWMeta interview questions
- Make a Sorted Array Unique In PlaceAlpaca · ONSITE INTERVIEW
- Two SumMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Maximize Distance to Closest Person — Return the SeatRoblox · ONSITE INTERVIEWRoblox interview questions
- Move ZeroesBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions