9. Merge Sorted Array
easyAsked at RedditMerge two sorted arrays into the first one in-place. Reddit uses this to test back-pointer technique — the same insight powering their in-place merging of ranked feed segments without allocating a new array per request.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Reddit loops.
- Glassdoor (2026-Q1)— Reddit feed-ranking phone-screen problem.
- Blind (2025-12)— Reported as the warm-up before merge-k-feeds question.
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. nums1 has length m + n where the last n elements are 0.
Constraints
nums1.length == m + nnums2.length == n0 <= m, n <= 2001 <= m + n <= 200-10^9 <= nums1[i], nums2[j] <= 10^9
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]Example 3
nums1 = [0], m = 0, nums2 = [1], n = 1[1]Approaches
1. Front-merge with copy
Copy nums1's first m elements aside, then merge from the front.
- Time
- O(m + n)
- Space
- O(m)
function merge(nums1, m, nums2, n) {
const copy = nums1.slice(0, m);
let i = 0, j = 0, k = 0;
while (i < m && j < n) nums1[k++] = copy[i] <= nums2[j] ? copy[i++] : nums2[j++];
while (i < m) nums1[k++] = copy[i++];
while (j < n) nums1[k++] = nums2[j++];
}Tradeoff: O(m) extra space. Anti-pattern when the question's premise is the trailing zeros.
2. Back-merge with three pointers (optimal)
Two read pointers (one per array) start at the last real elements; one write pointer starts at the end of nums1. Walk backward picking the larger.
- 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 'walk backward' insight is the entire signal.
Reddit-specific tips
Reddit interviewers grade on whether you spot that walking backward eliminates the overwrite problem. Bonus signal: mention that their feed-merger reuses this for hot+new+rising and that the back-walk lets them avoid allocating per-request buffers.
Common mistakes
- Forgetting to flush nums2 when nums1 is exhausted (j >= 0 must continue).
- Walking forward and overwriting unread nums1 values.
- Using nums1[i] >= nums2[j] (the > vs. >= doesn't matter here but matters for stability in other merges).
Follow-up questions
An interviewer at Reddit may pivot to one of these next:
- Merge k sorted arrays — switch to min-heap.
- What if nums1 doesn't have trailing zeros? Need to grow it.
- Merge intervals (LC 56).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is back-merge the canonical solution?
The trailing zeros in nums1 provide the buffer. Writing from the right means we never overwrite a read-not-yet-consumed value.
Why doesn't i >= 0 need to be in the outer loop?
If j hits -1, all of nums2 is placed; remaining nums1 values are already in position. Only when j > 0 do we need to keep writing.
Practice these live with InterviewChamp.AI
Drill Merge Sorted Array and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →