CodePark/ problems / Merge Two Sorted Arrays
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Foundations

Merge Two Sorted Arrays

easyabout 12 minutes in an interview

Write mergeSorted(a, b) that takes two arrays of numbers and returns one new array with all their numbers. Each input array is already sorted from smallest to largest. The result must also be sorted from smallest to largest.

  • Keep every number, including duplicates. Duplicates can be inside one array or across both.
  • Either array can be empty, or both can. Numbers can be negative.
  • Don't change a or b.
  • Don't use .sort. The inputs are already sorted, so you can do it in one pass.
Input: a = [1, 4, 6], b = [2, 3, 7] Output: [1, 2, 3, 4, 6, 7]
Input: a = [0, 5, 5], b = [5] Output: [0, 5, 5, 5]
Input: a = [], b = [-1, 2] Output: [-1, 2]