Merge Two Sorted Arrays
Write the code, see it run, pass the checks.
The problem
- easy
- Code
Write mergeSorted(a, b): a and b are each sorted in ascending order. Return a new array with every number from both, in ascending order, keeping repeats. Don't change a or b, and aim to walk through both arrays side by side in one pass. Either array may be empty. For example, mergeSorted([1, 4, 9], [2, 4, 5, 12]) is [1, 2, 4, 4, 5, 9, 12]. The checks include two arrays of 200,000 numbers, so inserting numbers one at a time into the middle of an array is too slow.
Your code must pass
- Merges two sorted arrays in order
- Keeps repeated numbers
- Handles empty arrays
- Leaves both inputs unchanged
- Is fast enough for two arrays of 200,000