Merge Sorted Array
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Bloomberg reportedly put Merge Sorted Array in front of candidates in October 2022, and with m+n up to 10^5 the lazy move of appending and re-sorting costs you O((m+n) log(m+n)) when a linear pass exists. It's an array problem with a two-pointer trick, and it's short enough to feel easy right up until you overwrite your own data. If your OA invite lands this week, you need the trick cold. And if your head goes blank mid-assessment, StealthCoder runs invisibly as a safety net and hands you the fill-from-the-back solution.
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: fill nums1 from the back. Put one pointer at m-1 (last real value in nums1), one at n-1 (last in nums2), and a write pointer at m+n-1. Compare the two values, drop the larger at the write pointer, and move that pointer and the write pointer left. Going forward would clobber unread values in nums1, which is the classic pitfall. Going backward never overwrites anything you still need, because the write position is always at or ahead of the nums1 read position. When nums1's pointer runs out, copy the rest of nums2. When nums2 runs out, you're done since nums1's leftovers are already in place. Edge cases: m = 0 (Example 2), n = 0, and duplicates, where ties should take from either side. That's O(m+n) time and O(1) extra space. If you blank during the live OA, StealthCoder is the hedge that gives you this loop on screen.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
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 passed his OA cold and still thinks the filter is broken.
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 Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Merge Sorted Array FAQ
How hard is Merge Sorted Array really?+
It's an easy-tier problem, but the in-place requirement trips people up. The logic is about ten lines. Most failures come from merging front to back and overwriting values in nums1 before they've been compared, not from algorithm complexity.
What's the trick to solving it in O(1) extra space?+
Merge from the end. Use three pointers: last real element of nums1, last element of nums2, and the last slot of nums1. Place the larger value at the write slot each step. The empty capacity at the back means nothing unread gets overwritten.
Can I just concatenate and sort?+
It works and passes small cases, but it's O((m+n) log(m+n)) and ignores that both inputs are already sorted. With m+n up to 10^5 it'd likely still run, but it signals you missed the point. Use the backward two-pointer merge instead.
What edge cases should I test before submitting?+
Test m = 0 with nums2 non-empty, as in Example 2. Test n = 0, where nums1 should stay unchanged. Also test all of nums2 being smaller than nums1, and duplicates across both arrays. The loop condition on nums2's pointer is where bugs hide.
How do I prepare for this in 48 hours?+
Write the backward merge from memory three times, then trace Example 1 by hand. Then do the sibling two-pointer problems like remove duplicates and two-sum on sorted input. Bloomberg reported this one in October 2022, so expect similar array questions with in-place constraints.