Reported July 2026
Amazontwo pointers

Merge Sorted Array

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

The whole Amazon Merge Sorted Array question, reported in July 2026, hinges on one thing: the spare room at the back of nums1. That buffer is the data structure. If you ignore it and allocate a new array, you've missed the point. It's an array problem with a two-pointer merge, and it's about as friendly as OA questions get. The catch is direction. Fill from the front and you overwrite values you still need. If you've got an invite and 48 hours, learn this one cold. StealthCoder sits invisibly on your screen during the live OA as a safety net if your mind goes blank.

The problem

You are given two integer arrays nums1 and nums2, each sorted in non-decreasing order, and two integers m and n representing the number of valid elements in the arrays.
Merge the valid elements of nums1 and all elements of nums2 into nums1 in non-decreasing order.
nums1 has length m + n. Its first m elements are valid input values, while its final n positions are placeholders that should be ignored before the merge.
nums2 has length n.
Modify nums1 in place, then return the merged nums1.

Function
mergeSortedArray(nums1: int[], m: int, nums2: int[], n: int) → int[]

Examples
Example 1
nums1 = [1,2,3,0,0,0]
m = 3
nums2 = [2,5,6]
n = 3
return = [1,2,2,3,5,6]
The valid portions are [1,2,3] and [2,5,6]. Their merged order is [1,2,2,3,5,6].
Example 2
nums1 = [1]
m = 1
nums2 = []
n = 0
return = [1]
The second array is empty, so nums1 remains unchanged.
Example 3
nums1 = [0]
m = 0
nums2 = [1]
n = 1
return = [1]
nums1 has no valid input elements. Its single position is merge capacity for the value from nums2.

Constraints
nums1.length == m + n
nums2.length == n
0 <= m, n <= 200
1 <= m + n <= 200
-10^9 <= nums1[i], nums2[j] <= 10^9
The first m elements of nums1 and all elements of nums2 are sorted in non-decreasing order.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to merge from the back. Put one pointer at index m-1 in nums1, one at n-1 in nums2, and a write pointer at m+n-1. Compare the two values, drop the larger one at the write pointer, and step that pointer back. The write pointer never catches up to unread data in nums1, so nothing gets clobbered. When nums1's pointer runs out, keep copying what's left of nums2. When nums2 runs out, you're done, because the rest of nums1 is already in place. Pitfalls: merging forward, forgetting the leftover nums2 loop, and mishandling m = 0 as in Example 3. Also don't sort the whole array after copying. It works, but it's the weak answer. O(m+n) time, O(1) space is what Amazon wants. If you freeze mid-assessment, StealthCoder can hand you the backward-pointer solution live.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Merge Sorted Array cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as merge sorted array. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Merge Sorted Array FAQ

How hard is Merge Sorted Array really?+

Easy. The logic is short, but the in-place requirement trips people up. If you merge from the front you overwrite data. Once you see the backward fill, it's ten lines. Most failures come from edge cases, not the idea.

What's the trick for the Amazon version?+

Start from the end. Three pointers: last valid element of nums1, last element of nums2, and the last slot of nums1. Place the larger value at the write slot each step. This keeps it O(1) extra space and avoids overwriting unread values.

Which edge cases should I test?+

Test m = 0 (nums1 is only placeholders), n = 0 (nums2 empty), and duplicates across both arrays. Also try all of nums2 being smaller than nums1. The loop must keep copying leftover nums2 elements after nums1's pointer goes negative.

Can I just concatenate and sort?+

It passes, since the constraints are tiny, but it's O(n log n) and ignores that both inputs are already sorted. In an Amazon OA the two-pointer answer is safer. Mention the sort approach as a fallback only if you're out of time.

How do I prepare for this in 48 hours?+

Write the backward merge from scratch twice, then run Examples 1 to 3 by hand. Spend the rest of your time on other array and two-pointer problems. This pattern shows up often, so the muscle memory pays off beyond this one question.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it