Intersection of Two Arrays
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
A hash set does almost all the work in this Bloomberg OA, reported in October 2020. The task is Intersection of Two Arrays: return the distinct integers that show up in both input arrays, sorted ascending. It looks like a warmup, and it is. But warmups are where people lose points by rushing and returning duplicates. Each array holds up to 10^5 signed 32-bit integers, so a nested loop is the wrong move. If your mind goes blank on the day, StealthCoder runs invisibly on your screen as a safety net. Know the set approach cold and you probably won't need it.
The problem
Return the distinct integers that appear in both first and second, sorted in ascending order. Function intersection(first: int[], second: int[]) → int[] Examples Example 1 first = [1,2,3,4] second = [2,4,6] return = [2,4] Only 2 and 4 occur in both arrays. Constraints Each array contains at most 10^5 signed 32-bit integers.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Dump the first array into a hash set. Walk the second array and collect any value that's in the set, storing hits in a result set so duplicates collapse. Then sort the result and return it. That's O(n + m) for the lookups plus O(k log k) for sorting the k matches. The common pitfall is returning duplicates, like [2,2,4] when the answer is [2,4]. The second pitfall is skipping the sort, because hash sets don't guarantee order. A third is the O(n*m) nested loop, which at 10^5 each means 10^10 comparisons and a timeout. The alternative is sorting both arrays and using two pointers, which skips the hash set but still needs dedup handling. Negative numbers are fine for sets. If you blank mid-assessment, StealthCoder is the hedge that hands you the set-plus-sort solution in seconds while the proctor sees nothing.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Intersection of Two 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 would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as intersection of two arrays. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Intersection of Two Arrays FAQ
How hard is Intersection of Two Arrays really?+
Easy. The logic is a set lookup and a sort. The only real risks are returning duplicates, forgetting the ascending order requirement, or writing a quadratic loop that times out at 10^5 elements. Get those three right and you're done in minutes.
What's the trick to this problem?+
Use a hash set for O(1) membership checks. Put one array in a set, scan the other, and store matches in a second set so each value appears once. Sort the matches at the end. That covers distinctness, speed, and ordering.
Should I use two pointers instead of a hash set?+
Either works. Two pointers means sorting both arrays, then advancing the smaller pointer and skipping repeats. It uses less extra memory but has more edge cases around duplicates. The hash set version is shorter and harder to get wrong under pressure.
Is this pattern still asked in OAs after Bloomberg's October 2020 report?+
Set-based intersection and dedup problems show up constantly as early or filler questions. The exact wording changes, but the hash set plus sort pattern transfers directly, so it's worth having memorized the night before.
How do I prepare for this in 48 hours?+
Write the solution from scratch twice in your language of choice. Test with duplicates, no overlap, empty arrays, and negative numbers. Confirm the output is sorted. That takes under an hour and covers everything this question can throw at you.