Reported November 2021
FlexTradetwo pointers

Merge Two Sorted Arrays

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

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

The edge case that breaks a naive merge is the empty array, and FlexTrade's Merge Two Sorted Arrays, reported in November 2021, puts it right in Example 2. If your loop assumes both inputs have elements, it crashes or drops values. This is a classic two-pointer merge on sorted arrays, with duplicates kept and inputs up to 200000 long each. It's easy, but easy problems punish sloppy tails. You've got an OA coming, so know the shape cold. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but this one you can hold in your head.

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 is two pointers, one per array. Compare first[i] and second[j], push the smaller, advance that pointer. Use <= when comparing so ties take from either side without losing a value. Duplicates are retained automatically because you never skip anything. The pitfall is the tail. When one array runs out, you must copy the rest of the other. Forgetting that, or only handling it for one side, fails Example 2 and any case where one array is exhausted early. Don't concatenate and sort. That's O(n log n) when O(n + m) is expected, and with 200000 elements each it's wasteful. Preallocate the result at size n + m. Negative values and 32-bit extremes don't matter since you only compare, never subtract. If you freeze on the OA, StealthCoder is the hedge that hands you the loop, but the logic is about ten lines.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

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 FlexTrade's OA.

FlexTrade 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 Two Sorted Arrays FAQ

How hard is the FlexTrade Merge Two Sorted Arrays question really?+

Easy. It's the merge step from merge sort. The only real risk is the leftover tail and empty inputs. If you can write two pointers and a cleanup loop, you're done in a few minutes. Test the empty-array example first.

What's the trick to this problem?+

Two pointers, one per array, always taking the smaller current value. After the main loop ends, copy whatever remains from whichever array isn't exhausted. Using <= on ties keeps every duplicate and stays stable.

Can I just concatenate and sort?+

It works for correctness but costs O(n log n) instead of O(n + m). With up to 200000 elements per array, a grader may expect the linear merge. Write the two-pointer version so you're safe either way.

What edge cases should I test before submitting?+

Test an empty first array, an empty second array, both empty, all duplicates like [2,2], negative numbers, and arrays where one is entirely smaller than the other. Each one exercises the tail-copy logic differently.

How do I prepare for this in 48 hours?+

Write the merge from scratch twice without looking. Then do the same with the tail loops moved into a single while condition. That covers this problem and its variants like merging into an existing array or merging k arrays.

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

OA at FlexTrade?
Invisible during screen share
Get it