Interview Q&A / Data Structures & Algorithms

BeginnerIn-depthCoding3 min readUpdated 2 Oct 2026

How do you merge two sorted arrays into one sorted array?

SHORT ANSWER

Use two pointers, one at the start of each array. Compare the two current elements, copy the smaller one to the result and move that pointer forward. When one array runs out, copy the rest of the other. This is O(n + m) time, much better than joining and sorting again.

Explain it simply

Two queues of students are already sorted by height, and you need one combined queue, also sorted. You do not need to shuffle everyone and start over. Just look at the front student of each queue, send the shorter one forward, and repeat. When one queue is empty, the rest of the other queue walks in as it is, because it is already sorted.

That is the merge step, and it is the heart of merge sort.

Line by line

js
function mergeSorted(a, b) {
  const result = [];
  let i = 0; // pointer into a
  let j = 0; // pointer into b

  while (i < a.length && j < b.length) {
    if (a[i] <= b[j]) {
      result.push(a[i]); // a's front is smaller (or equal), take it
      i++;
    } else {
      result.push(b[j]); // b's front is smaller, take it
      j++;
    }
  }

  // One array is finished; copy whatever is left of the other
  while (i < a.length) result.push(a[i++]);
  while (j < b.length) result.push(b[j++]);

  return result;
}

console.log(mergeSorted([1, 4, 7], [2, 3, 8, 9])); // Output: [1,2,3,4,7,8,9]
console.log(mergeSorted([1, 2, 2], [2, 5])); // Output: [1,2,2,2,5]
console.log(mergeSorted([], [3, 6])); // Output: [3,6]
console.log(mergeSorted([5], [])); // Output: [5]

Trace for [1, 4, 7] and [2, 3, 8, 9]:

CompareTakeResult so far
1 vs 211
4 vs 221, 2
4 vs 331, 2, 3
4 vs 841, 2, 3, 4
7 vs 871, 2, 3, 4, 7
a is emptycopy 8, 91, 2, 3, 4, 7, 8, 9

How it works

  • At every step, the smallest element not yet placed must be at the front of one of the two arrays, because each array is sorted. So taking the smaller front is always correct.
  • Using <= instead of < takes from the first array on ties. That keeps equal elements in their original order, which makes merge sort stable.
  • The two clean-up loops handle arrays of different lengths. Only one of them will actually run.

Complexity

ApproachTimeExtra space
Concatenate then sortO((n + m) log(n + m))depends on the sort
Two-pointer mergeO(n + m)O(n + m) for the result
In-place merge from the back (LeetCode 88 style)O(n + m)O(1)

Common mistakes

  • Forgetting to copy the leftover elements after the main loop, so the tail of the longer array is lost.
  • Using || instead of && in the main loop condition, which reads past the end of the shorter array.
  • Writing a.concat(b).sort() in JavaScript. Apart from being slower, the default sort() compares as strings, so [10, 9].sort() gives [10, 9]. You need sort((x, y) => x - y).
  • In the in-place version, filling from the front and overwriting values you still need.

What to say in the interview

"Since both arrays are already sorted, I use two pointers. I compare the current elements, push the smaller one to the result and advance that pointer. When one array is exhausted, I append the rest of the other. That is O(n plus m) time and O(n plus m) space for the output. If I am asked to merge into the first array, which has extra space at the end, I fill from the back so I never overwrite elements I still need, which uses O(1) extra space."

Follow-up questions

  • How do you merge into the first array in place when it has empty slots at the end?
  • How would you merge k sorted arrays efficiently?
  • How is this used inside merge sort?
  • How do you find the intersection of two sorted arrays?

Go deeper

Read these once the basics above feel easy.

In-place merge (LeetCode 88): nums1 has length m + n, with the last n slots empty. Start three pointers at the ends: i = m - 1, j = n - 1, k = m + n - 1. Place the larger of nums1[i] and nums2[j] at nums1[k] and move pointers left. Filling from the back is safe because the empty slots are at the back. When j drops below 0 you are done; leftovers in nums1 are already in place.

js
function mergeInPlace(nums1, m, nums2, n) {
  let i = m - 1, j = n - 1, k = m + n - 1;
  while (j >= 0) {
    if (i >= 0 && nums1[i] > nums2[j]) nums1[k--] = nums1[i--];
    else nums1[k--] = nums2[j--];
  }
  return nums1;
}

console.log(mergeInPlace([1, 2, 3, 0, 0, 0], 3, [2, 5, 6], 3)); // Output: [1,2,2,3,5,6]

#arrays#two-pointers#sorting#merge

Related questions

Practise more Data Structures & Algorithms questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Data Structures & Algorithms quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.