Merge Two Sorted Arrays In Place
Updated
Problem statement
You are given two nonempty integer arrays, first and second, each sorted in nondecreasing order.
Rearrange their existing elements using O(1) auxiliary storage so that both arrays remain sorted and every element of first is less than or equal to every element of second. Do not call a built-in sorting function.
Return the two mutated arrays as [first, second]. The returned outer array is only the judging container and does not relax the constant-extra-space requirement for the rearrangement.
Function
mergeSortedArrays(first: int[], second: int[]) → int[][]Examples
Example 1
first = [1,4,7,8,10]second = [2,3,9]return = [[1,2,3,4,7],[8,9,10]]The eight values in global sorted order are [1, 2, 3, 4, 7, 8, 9, 10]. The first five remain in first and the final three remain in second.
Example 2
first = [1,2,2]second = [2,3]return = [[1,2,2],[2,3]]Equal values may appear on both sides of the final boundary.
Constraints
1 <= first.length <= 2000001 <= second.length <= 200000- Both arrays are initially sorted in nondecreasing order.
- Every value fits in a signed 32-bit integer.
- Duplicate values are allowed.
- Use
O(1)auxiliary storage and do not call a built-in sorting function.
More Infosys Onsite Interview questions
- Minimum Coins for a TargetONSITE INTERVIEW · Seen Nov 2026
- Delete Duplicate Employee RecordsONSITE INTERVIEW · Seen Nov 2026
- Find Duplicate Employee RecordsONSITE INTERVIEW · Seen Nov 2026
- Minimum Insertion Steps to Make a String PalindromeONSITE INTERVIEW · Seen Nov 2026
- Maximize the Even-Odd Index Product GapONSITE INTERVIEW · Seen Oct 2026
- Minimum Nonadjacent Flips for an Alternating Binary StringONSITE INTERVIEW · Seen Oct 2026
- Tenth-Highest Distinct SalaryONSITE INTERVIEW · Seen Oct 2026
- Assign Cookies With Matching ParityONSITE INTERVIEW · Seen Sep 2026
- Count Equal Code Pairs Within Distance KONSITE INTERVIEW · Seen Sep 2026
- Daily Successful Transactions Per DepartmentONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Top K With a Deterministically Noisy Comparator (MLE)Mercor · ONSITE INTERVIEWMercor interview questions
- Recursive Display SortFigma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
- Russian Doll EnvelopesGoogle · ONSITE INTERVIEWGoogle interview questions
- Process Shopping Records, Promotions, and Display OrderInstacart · PHONE SCREEN · ONSITE INTERVIEWInstacart interview questions
- Employee Free TimeIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
- Minimum Operations to Make an Array ContinuousAmazon · ONSITE INTERVIEW
- Minimum Refueling StopsUber · ONSITE INTERVIEWUber interview questions
- Russian Doll EnvelopesInMobi · ONSITE INTERVIEWInMobi interview questions
- Beam Search and Nucleus DecodingMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Partition into Equal-Sum GroupsPalo Alto Networks · ONSITE INTERVIEWPalo Alto Networks interview questions