Merge Sorted Arrays with Duplicates
Reported by candidates from Mygate's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Mygate OA reported in January 2019 hands you two sorted arrays and one rule: keep every duplicate. In the first example, 2 shows up twice in a and once in b, so it lands three times in the output. That's the whole question. It's a clean two-pointer merge, the same step inside merge sort. If you've written it before, this is ten minutes. If you blank on pointer edge cases, StealthCoder is the safety net running invisibly during the live OA. Most people lose points on empty arrays and leftover tails, not on the idea.
The problem
Given two integer arrays a and b, each sorted in nondecreasing order, return a new array containing all their elements in nondecreasing order. Retain every occurrence of every value, including duplicates within an input or across both inputs. Do not change either input. Either array, or both, may be empty. Function mergeSortedArrays(a: int[], b: int[]) → int[] Examples Example 1 a = [1,2,2] b = [2,3] return = [1,2,2,2,3] The value 2 occurs twice in a and once in b, so it occurs three times in the result. Example 2 a = [] b = [-2,0,0] return = [-2,0,0] An empty first array contributes no elements. Constraints 0 ≤ a.length, b.length ≤ 10,000. −100,000,000 ≤ every array value ≤ 100,000,000. Both input arrays are sorted in nondecreasing order.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Use two pointers, one per array. Compare a[i] and b[j], push the smaller into the result, advance that pointer. On a tie, take either one. Since you keep duplicates, you never skip or dedupe. When one array runs out, append the rest of the other. That handles the empty cases from Example 2 for free. The pitfall is building the result with a loop that stops too early and drops the tail. Another is mutating the inputs, which the statement forbids, so don't pop or splice. Complexity is O(n + m) time and O(n + m) space. With up to 10,000 elements each, don't concatenate and sort. It works but wastes the sorted guarantee. If your mind goes blank mid-assessment, StealthCoder can supply the merge loop as a hedge, but this one is easy to own yourself.
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 Arrays with Duplicates 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 Mygate's OA.
Mygate 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 Arrays with Duplicates FAQ
How hard is the Mygate merge sorted arrays question really?+
Easy. It's the merge step from merge sort. Two pointers, one pass, and a tail copy. The only real risk is sloppy edge cases like empty arrays or forgetting leftover elements after one input is exhausted.
What's the trick to keep duplicates correctly?+
Don't dedupe anything. On every comparison, take the smaller value and advance that pointer. If values are equal, taking either one is fine because the other gets taken next. Every occurrence from both inputs ends up in the result.
Can I just concatenate and sort?+
It passes on correctness, but it's O(n log n) and ignores the sorted input guarantee. An interviewer or hidden test may expect the linear merge. Write the two-pointer version, it's barely more code and clearly the intended answer.
What edge cases should I test before submitting?+
Test both arrays empty, one empty, all equal values across both, negatives like -2 and 0, and one array fully smaller than the other. Also confirm you didn't modify the inputs, since the statement says not to change either one.
How do I prepare for this in 48 hours?+
Write the merge loop from memory three times, once in your OA language. Then do the variants: merge in place, merge k arrays, and intersection of sorted arrays. That covers the pointer patterns this question tests.