Asked at 12 companies
8 recent
- Meta
- EPAM Systems
- Bloomberg
- tcs
- Microsoft
- Amazon
- Yandex
- Squarespace
- Avito
+2 moreShow fewer
- Hubspot
- HCL
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Indexing and walking through an array · Building a new string or array as the answer · Partitioning around a pivot · Sorting costs O(n log n)
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · Merging sorted data: Databases join two sorted tables by walking a pointer down each one at once, the same move as merging two sorted arrays.
ProblemProblem statementYou 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 nums2 into nums1 as one sorted array. nums1 has length m + n: its first m elements are the real values and its last n elements are 0s, there only as space for the merge. Do it in place — do not return anything, just modify nums1.ReadHide
Problem Statement
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 nums2 into nums1 as one sorted array. nums1 has length m + n: its first m elements are the real values and its last n elements are 0s, there only as space for the merge. Do it in place — do not return anything, just modify nums1.
Added 2026-10-30 from Srujan's LeetCode solved list (photo) — it was open as a browser tab in the photo but not previously in the repo.
Input / Output
Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
Input: nums1 = [1], m = 1, nums2 = [], n = 0
Output: [1]
Input: nums1 = [0], m = 0, nums2 = [1], n = 1
Output: [1]
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Pattern Recognition
Merge from the End — merging from the front would overwrite values in nums1 you still need. The empty 0 slots sit at the end, so work backwards: at each step, the largest remaining element (the larger of the two tails) belongs in the last free slot. That slot is always safe to write, because everything after it is already placed.
Approach
Brute Force — Copy, Then Sort, O((m + n) log(m + n))
nums1[m:] = nums2 # fill the empty tail with nums2
nums1.sort() # sort the whole array
Optimized — Three Pointers from the End, O(m + n) time, O(1) space
p1 = m - 1 # last real element of nums1
p2 = n - 1 # last element of nums2
p = m + n - 1 # last free slot of nums1
while p2 >= 0:
if p1 >= 0 and nums1[p1] > nums2[p2]:
nums1[p] = nums1[p1]
p1 -= 1
else:
nums1[p] = nums2[p2]
p2 -= 1
p -= 1
When nums2 is exhausted, anything left in nums1 is already in place — so the loop only needs to run until p2 runs out. (If p1 runs out first, the remaining nums2 elements just copy straight across.)
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Copy, then sort | O((m+n) log(m+n)) | O(1) | Ignores that both are sorted |
| Three pointers, end | O(m + n) | O(1) | In place, uses the free tail |
Solution code
from typing import List
class SolutionBruteForce:
"""O((m+n) log(m+n)) time | O(1) space — copy nums2 into the tail, then sort."""
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
nums1[m:] = nums2
nums1.sort()
class Solution:
"""O(m+n) time | O(1) space — three pointers, merge from the end."""
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
p1 = m - 1
p2 = n - 1
p = m + n - 1
while p2 >= 0:
if p1 >= 0 and nums1[p1] > nums2[p2]:
nums1[p] = nums1[p1]
p1 -= 1
else:
nums1[p] = nums2[p2]
p2 -= 1
p -= 1Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language