Merge Sorted Array
Reported by candidates from Motive's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Motive OA reported in April 2024 asks for Merge Sorted Array, and the trap is the one that wrecks a naive solution: merging from the front overwrites values in nums1 you haven't read yet. It's an array problem with two sorted inputs and spare room at the end of the first one. You've seen it, or something close. The risk is a rushed pass that fails on the empty case, like m = 0. If you blank on the direction of the loop mid-assessment, StealthCoder is the safety net running invisibly on your screen. Know the trick first and you won't need it.
The problem
nums1 has length m+n. Its first m values and all n values of nums2 are sorted nondecreasingly; the remaining positions of nums1 are capacity. Merge both sorted sequences into nums1 and return it. 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 two sorted prefixes merge in nondecreasing order. Example 2 nums1 = [0] m = 0 nums2 = [1] n = 1 return = [1] The first sequence is empty. Constraints 0 <= m, n and 1 <= m+n <= 10^5. nums1.length == m+n and nums2.length == n.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to fill nums1 from the back. Use three pointers: i at m-1, j at n-1, and k at m+n-1. Compare nums1[i] and nums2[j], write the larger at k, then move that pointer and k down. Writing from the back means you never overwrite an unread element of nums1. The loop runs while j >= 0. Once nums2 is exhausted, the rest of nums1 is already in place, so you don't copy anything. The common pitfall is looping on i >= 0 and forgetting that when m = 0, nums2 still has to be copied in. Example 2 tests exactly that. Also guard i < 0 before indexing nums1. Time is O(m+n), space is O(1). If the assessment clock gets tight and your pointer logic goes fuzzy, StealthCoder can hand you the clean version live, but this one is short enough to write from memory.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as merge sorted array. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Motive's OA.
Motive reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Merge Sorted Array FAQ
How hard is Merge Sorted Array really?+
It's easy on paper, but the in-place requirement catches people. The logic is about ten lines. Most failures come from merging forward and overwriting data, or mishandling the case where nums1 has no real elements. Get the backward pointers right and it's done.
What's the trick to solving it in place?+
Fill from the end. Put the largest remaining value at index m+n-1 and work backward. The empty tail of nums1 gives you free space, so no unread element gets overwritten. Forward merging needs extra memory or shifting, which is slower.
Which edge cases should I test before submitting?+
Test m = 0 with nums2 filled, like Example 2. Test n = 0 where nums1 stays untouched. Test all of nums2 being smaller than nums1, and duplicates across both arrays. Those four cover nearly every bug in the backward-pointer approach.
Can I just concatenate and sort?+
It passes correctness, since you copy nums2 into the tail and sort. But that's O(n log n) and ignores that both inputs are already sorted. An interviewer or hidden test may expect the O(m+n) two-pointer merge, so write that one.
How do I prepare for this in 48 hours?+
Write the backward three-pointer merge from scratch twice without looking. Then trace Example 1 and Example 2 by hand. That's enough. Spend the remaining time on other array and two-pointer patterns, since Motive's April 2024 report suggests this style of question shows up.