Merge Sorted Array is the #14 most asked LeetCode problem globally — the classic in-place merge problem that tests whether you can think backwards.
Our analysis of 10,385 verified interview questions across 259 companies shows it appeared 65 times across 26 companies — including Google, Amazon, Meta, Microsoft, Goldman Sachs, Bloomberg, LinkedIn, ByteDance, and Expedia.
The naive approach copies elements and wastes O(m+n) space. The elegant solution merges from the end — placing the largest elements first into the buffer space already available at the tail of nums1. O(m+n) time, O(1) space.
| Metric | Value |
|---|---|
| Total Interview Appearances | 65 verified mentions |
| Companies That Ask It | 26 companies |
| Global Rank | #14 out of 10,385 questions |
| Difficulty | Easy |
| Core Patterns | Array, Two Pointers, Sorting |
Google, Amazon, Meta, Microsoft, Goldman Sachs, Bloomberg, LinkedIn, ByteDance, Expedia, Adobe, Agoda, Apple, Atlassian, Avito, Barclays, Cisco, Cognizant, Criteo, EPAM Systems, HCL, Hubspot, IBM, Infosys, Intel, and 2 more.
LeetCode #88 — Merge Sorted Array
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. nums1 has a length of m + n, where the last n elements are set to 0. Merge nums2 into nums1 in-place sorted order.
Input: nums1 = [1,2,3,0,0,0], m=3, nums2 = [2,5,6], n=3
Output: nums1 = [1,2,2,3,5,6]
Input: nums1 = [1], m=1, nums2 = [], n=0
Output: nums1 = [1]
Input: nums1 = [0], m=0, nums2 = [1], n=1
Output: nums1 = [1]
Merging from the front causes overwrites — nums1[0] gets clobbered before you use it. Merging from the back avoids this entirely: the tail of nums1 (positions m to m+n-1) is all zeros — empty buffer space.
Use three pointers: p1 = m-1 (last real element in nums1), p2 = n-1 (last element in nums2), p = m+n-1 (write position). At each step, place the larger of nums1[p1] and nums2[p2] at position p and move the corresponding pointer left.
We're writing from right to left into the buffer. We can never overwrite an element we still need, because the write pointer p always stays ahead of (or at) the read pointer p1.
def merge(nums1: list[int], m: int, nums2: list[int], n: int) -> None:
p1 = m - 1 # pointer for nums1 real elements
p2 = n - 1 # pointer for nums2
p = m + n - 1 # write pointer (end of nums1)
while p1 >= 0 and p2 >= 0:
if nums1[p1] > nums2[p2]:
nums1[p] = nums1[p1]
p1 -= 1
else:
nums1[p] = nums2[p2]
p2 -= 1
p -= 1
# If nums2 has remaining elements, copy them
# (nums1 remaining elements are already in place)
while p2 >= 0:
nums1[p] = nums2[p2]
p2 -= 1
p -= 1
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int p1 = m - 1, p2 = n - 1, p = m + n - 1;
while (p1 >= 0 && p2 >= 0) {
if (nums1[p1] > nums2[p2])
nums1[p--] = nums1[p1--];
else
nums1[p--] = nums2[p2--];
}
while (p2 >= 0)
nums1[p--] = nums2[p2--];
}
}
var merge = function(nums1, m, nums2, n) {
let p1 = m - 1, p2 = n - 1, p = m + n - 1;
while (p1 >= 0 && p2 >= 0) {
if (nums1[p1] > nums2[p2])
nums1[p--] = nums1[p1--];
else
nums1[p--] = nums2[p2--];
}
while (p2 >= 0)
nums1[p--] = nums2[p2--];
};
[1,2,3,0,0,0] + [2,5,6] step by step.Tag on DSAPrep.dev under Two Pointers and Sorting.