Reported December 2020
Bloombergtwo pointers

Merge Two Sorted Streams with Next Calls

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

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

The classic way to fumble this Bloomberg OA, reported in December 2020, is to dedupe the 4s and return [1,2,4,7,8] instead of [1,2,4,4,7]. The task is a two-pointer merge, but you stop after calls values instead of draining both arrays. It's the merge step from merge sort with a cap. If you've got an OA invite in your inbox, this one is quick once you stop overthinking it. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the logic here fits in about ten lines.

The problem

first and second are nondecreasing finite streams. A stateful next() returns the smaller current head, advancing that stream; equal values are both preserved, with first chosen before second on a tie.
Return the first calls values produced.

Function
mergeNextValues(first: int[], second: int[], calls: int) → int[]

Examples
Example 1
first = [1,4,7]
second = [2,4,8]
calls = 5
return = [1,2,4,4,7]
Five next calls preserve both copies of 4.

Constraints
0 <= calls <= first.length + second.length.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Keep two indexes, i for first and j for second. Loop exactly calls times. Each iteration, compare first[i] and second[j]. If first[i] <= second[j], take from first, else take from second. The <= is the tie rule: first wins on equal values, and the other copy stays put for a later call. The pitfalls are bounds. When one array is exhausted, you must take from the other without indexing past the end. Check i < first.length and j < second.length before comparing. Don't dedupe, and don't merge everything then slice, though that still passes. Stopping early is cleaner and O(calls). The calls constraint guarantees you never run out of values. If you freeze on the exhausted-array branch during the live OA, StealthCoder is the hedge that hands you the clean loop.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Merge Two Sorted Streams with Next Calls 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 StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Bloomberg's OA.

Bloomberg 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 Streams with Next Calls FAQ

How hard is the Bloomberg merge two sorted streams problem really?+

Easy. It's the merge step from merge sort with a stop condition. The only real risks are index bounds when one array runs out and getting the tie rule backward. If you've written a two-pointer merge before, expect a few minutes of work.

What's the trick to this problem?+

Two pointers and a loop that runs exactly calls times. Each pass, pick the smaller head, advance that pointer, and append the value. On a tie, take from first. There's no sorting and no extra data structure needed.

Do I remove duplicates when both streams have the same value?+

No. The example keeps both 4s, giving [1,2,4,4,7]. A tie means you take first's copy now, and second's copy comes out on a later call. Using a set here is the most common first-attempt mistake.

How do I handle one array running out early?+

Guard each comparison. If i has reached first.length, take from second. If j has reached second.length, take from first. The constraint calls <= first.length + second.length means you never run out of total values, so no error handling is needed.

How should I prepare for this in 48 hours?+

Write the merge loop from memory twice, once in full and once capped at calls. Then test edge cases: calls = 0, one empty array, all ties, and one array fully consumed first. That covers nearly every failure mode for this problem.

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

OA at Bloomberg?
Invisible during screen share
Get it