Skip to main content

8. Merge Sorted Array

easyAsked at Figma

Given two sorted arrays nums1 (with extra space) and nums2, merge them into nums1 in sorted order. Figma asks this to test the 'merge from the back' trick, which mirrors how their CRDT log appends two reconciled histories without an auxiliary buffer.

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

Source citations

Public interview reports confirming this problem appears in Figma loops.

  • Glassdoor (2026-Q1)Recurring Figma onsite — they specifically want O(1) extra space.
  • LeetCode Discuss (2025-11)Figma OA easy section pairs this with merge-two-lists.

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 not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n.

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]

Approaches

1. Concatenate + sort

Copy nums2 into nums1's tail; sort the whole thing.

Time
O((m+n) log(m+n))
Space
O(1)
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-ness of the inputs. Don't ship this in an interview.

2. Three pointers from the back

Walk from the END: place the larger of nums1[m-1] / nums2[n-1] at nums1[m+n-1]. Empty cells in nums1 never get clobbered before they're read.

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 because the trailing zeros in nums1 are the workspace. Walking from the back is the entire trick.

Figma-specific tips

Figma will dock you if you reach for sort() — they want the back-pointer trick because it mirrors how their op-log merges two reconciled histories without copying. Articulate 'the tail of nums1 is empty, so we fill from the back and never overwrite an unread cell.' Bonus points for pointing out you only need to loop while j >= 0 (anything left in nums1 is already in place).

Common mistakes

  • Walking forward and clobbering nums1's unread elements.
  • Looping while i >= 0 instead of j >= 0 — if nums1 elements remain they're already in place.
  • Forgetting m+n-1 as the initial write index.

Follow-up questions

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

  • Merge Two Sorted Lists (LC 21).
  • Merge k Sorted Lists (LC 23).
  • Merge intervals (LC 56).

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 walk from the back?

The empty space sits at the back. Filling from the back means you write into cells that have already been read (or were always empty), so you never need an auxiliary buffer.

Why does the loop terminate when j < 0 and not i < 0?

If j runs out, anything left in nums1 is already at the correct position from the original sort — no further work needed. If i runs out but j hasn't, the else branch handles those copies.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →