Explain merging.
Skip to solutionKEEP THE
mediumDSA
How do you merge two sorted arrays?
317 views
01
Understand the problem
sortingmerge
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
Use two pointers, one per array, repeatedly appending the smaller current element to the result — O(m+n). When merging into one array in place (from the back) you avoid extra space, a common interview twist.
Solution ready — 2 min read
Classified // press E to declassify
04
Read the code
Two-pointer merge
Run Playgrounddef merge_sorted(a, b):
i = j = 0
out = []
while i < len(a) and j < len(b):
if a[i] <= b[j]:
out.append(a[i]); i += 1
else:
out.append(b[j]); j += 1
out.extend(a[i:])
out.extend(b[j:])
return out
# --- demo ---
print(merge_sorted([1, 3, 5], [2, 4, 6])) # [1, 2, 3, 4, 5, 6]05
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 82 of 127 decoded in the Data Structures & Algorithms track. One more won't hurt.