Merge Two Sorted Arrays
Reported by candidates from Capgemini's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Capgemini reported this one in September 2026, and the input size is the whole point. Each array can hold 200000 values, so a lazy concat-and-sort works but throws away the fact that both inputs are already sorted. It's a classic two-pointer merge, the same step that sits inside merge sort. If you've got an OA invite for Capgemini this week, expect something this clean and expect the grader to care about linear time. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but this one is short enough to own before you start.
The problem
Given two integer arrays first and second, each sorted in non-decreasing order, return a new array containing every value from both inputs in non-decreasing order. Retain every occurrence of a value, including duplicates within one input or across both inputs. Function mergeSortedArrays(first: int[], second: int[]) → int[] Examples Example 1 first = [1,2,4] second = [1,3,4] return = [1,1,2,3,4,4] The two values equal to 1 and the two values equal to 4 are all retained in sorted order. Example 2 first = [] second = [2,2] return = [2,2] The first array is empty, so the result contains both values from second. Example 3 first = [-5,-1,0] second = [-3,2] return = [-5,-3,-1,0,2] Values from the two arrays alternate in the merged ordering. Constraints 0 <= first.length <= 200000 0 <= second.length <= 200000 Every value is a signed 32-bit integer. Both input arrays are sorted in non-decreasing order.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: keep one pointer per array, compare the current elements, and append the smaller one to the output. When one array runs out, copy the rest of the other. That's O(n + m) time and O(n + m) space for the result. Use less-than-or-equal when comparing so duplicates from both arrays are retained, which the problem explicitly demands. Common pitfalls: forgetting the empty-array case (Example 2 has an empty first array), forgetting to drain the leftover tail, and using a language's sort on the concatenation, which is O(n log n) and may get flagged on 400000 total elements. Also watch overflow if you do arithmetic on values. Just compare, never subtract. If your mind goes blank during the live OA, StealthCoder can hand you the two-pointer loop in seconds, but you can write this from memory.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Merge Two Sorted Arrays 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
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 Capgemini's OA.
Capgemini reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Merge Two Sorted Arrays FAQ
How hard is Merge Two Sorted Arrays really?+
Easy. It's one loop with two pointers and a tail copy. The Capgemini version reported in September 2026 adds no twist beyond keeping duplicates and handling empty inputs. Most candidates lose points on edge cases, not on the idea.
What's the trick to get full marks?+
Use two pointers and always take the smaller current value, then append whatever remains from the other array. That gives linear time. Sorting the concatenated array is correct but ignores the sorted input, and it may be slower on 200000-element arrays.
Do I need to remove duplicates?+
No. The problem says to retain every occurrence, including duplicates within one array or across both. In Example 1, both 1s and both 4s stay. Don't use a set. Just compare with less-than-or-equal and append.
What edge cases should I test before submitting?+
Test an empty first array, an empty second array, and both empty. Test all values in one array being smaller than the other. Test negatives like Example 3, and repeated values like [2,2]. Check that the leftover tail gets copied after the main loop ends.
How do I prepare in 48 hours for this kind of OA?+
Write the two-pointer merge from scratch three times until it's automatic. Then do a few related array problems: remove duplicates, intersection of arrays, and merge intervals. Focus on boundary handling and clean loops, since problems like this reward speed and precision.