Skip to main content

8. Merge Sorted Array

easyAsked at Asana

Merge two sorted arrays in-place, where the first has extra trailing slots. Asana asks this to test whether you spot the back-to-front trick — they care because the same insight powers their activity-feed merge logic.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Asana loops.

  • Glassdoor (2026-Q1)Asana phone-screen array warmup.
  • Blind (2025-08)Reported as a recurring Asana onsite warmup.

Problem

You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should be stored inside nums1.

Constraints

  • nums1.length == m + n
  • nums2.length == n
  • 0 <= m, n <= 200
  • 1 <= m + n <= 200
  • -10^9 <= nums1[i], nums2[j] <= 10^9

Examples

Example 1

Input
nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output
[1,2,2,3,5,6]

Example 2

Input
nums1 = [1], m = 1, nums2 = [], n = 0
Output
[1]

Example 3

Input
nums1 = [0], m = 0, nums2 = [1], n = 1
Output
[1]

Approaches

1. Concatenate and sort

Copy nums2 into the trailing slots, then sort.

Time
O((m+n) log (m+n))
Space
O(1) extra (in-place sort)
function merge(nums1, m, nums2, n) {
  for (let i = 0; i < n; i++) nums1[m + i] = nums2[i];
  nums1.sort((a,b)=>a-b);
}

Tradeoff: Throws away the sorted-input freebie. Asana flags this as a missed optimization.

2. Two pointers from the back

Fill from index m+n-1 backward. Pick the larger of nums1[i] and nums2[j] each step.

Time
O(m+n)
Space
O(1)
function merge(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--];
  }
}

Tradeoff: O(1) extra space — the trailing zeros become the destination buffer. No overwrites because k > i always while j is alive.

Asana-specific tips

Asana grades on whether you spot 'fill from the back.' Going front-to-back forces you to shift nums1 elements right, which is O(m*n). Articulate the invariant 'k > i' before coding — it's the proof that you never clobber unread data.

Common mistakes

  • Going front-to-back and shifting nums1 — O(m*n).
  • Stopping when i reaches -1 instead of when j does — leaves nums2 elements unmerged.
  • Off-by-one on the starting indices.

Follow-up questions

An interviewer at Asana may pivot to one of these next:

  • Merge k sorted arrays (LC 23 variant).
  • What if nums1 didn't have extra space?
  • Generalize to merging two sorted streams of unknown length.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why does back-to-front avoid overwrites?

k starts at m+n-1 and i starts at m-1; k > i strictly. Each iteration both decrement, but k decrements every step while i only decrements when nums1[i] wins, so k stays ahead of i.

Why is the loop conditioned on j >= 0, not i >= 0?

If j runs out, all remaining nums2 elements are placed and any leftover nums1 elements are already in their final positions. If i runs out first, you still need to copy remaining nums2.

Practice these live with InterviewChamp.AI

Drill Merge Sorted Array and other Asana interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →