Reported January 2021
Bloombergtwo pointers

Target Sum Across Two Sorted Arrays

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 data structure here is just the arrays themselves, and that's the whole joke. Bloomberg reported this OA in January 2021: two sorted arrays, one target, pick one value from each. Candidates see two arrays and reach for a hash set, which works but wastes the one thing the problem hands you for free, the sorted order. Two pointers solves it in linear time with no extra memory. If you blank on the pointer movement mid-assessment, StealthCoder runs invisibly on your desktop and gives you the solution live, so one bad minute doesn't sink the whole OA.

The problem

You are given two integer arrays first and second, each sorted in nondecreasing order, and a signed 64-bit integer target.
Return true if you can choose exactly one value from first and exactly one value from second whose sum equals target. Otherwise, return false. An empty input array therefore always produces false. Compute every candidate sum in signed 64-bit arithmetic.

Function
hasCrossArrayTargetSum(first: int[], second: int[], target: long) → boolean

Examples
Example 1
first = [1,3,5]
second = [2,4,8]
target = 9
return = true
The values 1 and 8 form the target.
Example 2
first = []
second = [1,2,3]
target = 4
return = false
No pair exists when either array is empty.
Example 3
first = [2147483647]
second = [2147483647]
target = 4294967294
return = true
The pair sum is computed in signed 64-bit arithmetic.

Constraints
0 <= first.length, second.length <= 200000.
Both arrays are sorted in nondecreasing order.
Every array value is a signed 32-bit integer.
-2^32 <= target <= 2^32 - 2.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Put one pointer at the start of first and one at the end of second. Compute the sum as a 64-bit value. If it equals target, return true. If it's too small, move the first pointer right to grow the sum. If it's too big, move the second pointer left to shrink it. Stop when either pointer runs out. That's O(n + m) time and O(1) space. The pitfalls are specific. Example 3 sums two 2147483647 values, which overflows a 32-bit int, so cast to long before adding. Empty arrays must return false, and the loop condition handles that naturally if you don't index first. A hash set also passes, but it's the weaker answer. If the pointer logic slips under pressure, StealthCoder is the hedge during the live OA. It reads the prompt and hands you the working code.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Target Sum Across 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Target Sum Across Two Sorted Arrays FAQ

What's the trick in the Bloomberg Target Sum Across Two Sorted Arrays question?+

Use two pointers. Start at the beginning of the first array and the end of the second. A sum that's too small advances the left pointer, a sum that's too big retreats the right one. It runs in linear time with constant space and uses the sorted order directly.

Is a hash set acceptable here?+

It's correct and runs in linear time, so it should pass. But it uses O(n) extra space and ignores the sorted property the problem gives you. Two pointers is the cleaner answer and the one an interviewer expects to see.

What overflow trap should I watch for?+

Each value fits in 32 bits, but the sum of two can exceed that. Example 3 adds two 2147483647 values to reach 4294967294. Cast to long or a 64-bit type before adding, and keep target as 64-bit too. Skipping the cast gives wrong answers on edge tests.

How do empty arrays affect the solution?+

If either array is empty, no pair exists, so return false. A standard two-pointer loop handles this if you set the right pointer to length minus one and loop while both indexes are valid. Just don't read an element before checking bounds.

How do I prepare for this in 48 hours?+

Write the two-pointer pair-sum loop from memory three times, once with an overflow cast and once with empty inputs. That covers the whole problem. Then skim related sorted-array pair problems so the pointer movement rule feels automatic.

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