9. Merge Sorted Array
easyAsked at CoinbaseMerge two sorted arrays in place, with nums1 sized large enough to hold both. Coinbase asks this because in-place merges show up everywhere in matching engines — the destination buffer is already allocated.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Coinbase loops.
- Glassdoor (2026-Q1)— Coinbase backend phone-screen warm-up.
- Blind (2025-08)— Frequently follows with 'merge price ladders from two venues'.
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. To accommodate this, nums1 has a length of m + n.
Constraints
nums1.length == m + nnums2.length == n0 <= m, n <= 2001 <= m + n <= 200
Examples
Example 1
nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3[1,2,2,3,5,6]Example 2
nums1 = [1], m = 1, nums2 = [], n = 0[1]Approaches
1. Copy nums2 into nums1 then sort
Splice nums2 into the trailing zeros, call sort.
- 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-input invariant. Easy to write, wrong choice in interviews.
2. Three-pointer from the end
Walk three pointers from the back: i in nums1 (last real value), j in nums2, k at the end of nums1. Place max(nums1[i], nums2[j]) at k and decrement.
- 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: Filling from the end avoids overwriting nums1 values you haven't read yet. Linear and in place.
Coinbase-specific tips
Coinbase wants you to articulate WHY back-to-front: forward would overwrite unread values in nums1. The same pattern appears when merging incremental order-book snapshots — bonus signal if you call that out. They sometimes ask 'what if nums2 may have duplicates of nums1 prices?' — clarify whether duplicates are merged or kept separate.
Common mistakes
- Walking forward and overwriting nums1 — classic bug.
- Forgetting to handle the case where nums2 still has elements after i < 0 — the while loop guards on j>=0 for exactly this reason.
- Not handling m=0 — the loop must still copy nums2 over.
Follow-up questions
An interviewer at Coinbase may pivot to one of these next:
- Merge K sorted price ladders (min-heap).
- What if duplicates by price should sum quantities instead of being kept as separate levels?
- Streaming merge — values arrive over time.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why three pointers, not just two and an index?
Three lets us walk i and j independently while k tracks the write head. With two you have to recompute k from i+j+1, which is error-prone.
What's the order-book analog?
Merging an incremental update (nums2) into a snapshot ladder (nums1) at the same price grid. Same shape, different interpretation of duplicates.
Practice these live with InterviewChamp.AI
Drill Merge Sorted Array and other Coinbase interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →