Two Sum
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that kills the naive Two Sum is the duplicate. Bloomberg reported this one in June 2022, and the twist is the [-1, -1] return plus the requirement that i < j. If you're taking the OA in a day or two, you probably know the hash map answer already. The risk is blanking on details under a timer. Nums can hit 10^5 entries, so a nested loop is out. This is a hash-table problem, and it's a warm-up, so you can't afford to miss it. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank mid-assessment.
The problem
Given an integer array nums and an integer target, return any pair of indices [i, j] such that i < j and nums[i] + nums[j] = target. If several valid pairs exist, you may return any one of them. If no valid pair exists, return [-1, -1]. Function twoSum(nums: int[], target: int) → int[] Examples Example 1 nums = [2,7,11,15] target = 9 return = [0,1] nums[0] + nums[1] = 2 + 7 = 9, and 0 < 1. Example 2 nums = [1,2,3] target = 7 return = [-1,-1] No pair of distinct indices has values that sum to 7. Constraints 1 <= nums.length <= 10^5. -10^9 <= nums[i] <= 10^9. -10^9 <= target <= 10^9.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is one pass with a hash map from value to index. For each number, compute target minus that number, check the map, and return the stored index plus the current one. Insert the current number only after the check. That order handles the classic pitfall: nums = [3,3] with target 6 works, and a single 3 with target 6 doesn't pair with itself. It also guarantees i < j, since the stored index is always earlier. If the loop finishes, return [-1, -1]. Don't forget that fallback, since the problem says any valid pair is fine but no pair means that sentinel. Values reach 10^9 in magnitude, so the sums can overflow a 32-bit int in some languages. Use a wider type for the sum if you add values directly. The complement approach with subtraction mostly avoids it. If you freeze on the ordering or the fallback, StealthCoder can hand you the working solution live.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Two Sum 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as two sum. 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Two Sum FAQ
How hard is Bloomberg's Two Sum really?+
Easy. It's the standard hash map problem with one small change: return [-1, -1] when no pair exists. The reported June 2022 version also states i < j explicitly. If you know the one-pass map, you finish in minutes.
What's the trick to avoid using the same element twice?+
Check the map for the complement before inserting the current number. That way an element can never match itself. Duplicates like [3,3] with target 6 still work, because the first 3 is stored by the time the second one is checked.
Can I brute force it with two loops?+
Not safely. Nums can have up to 10^5 elements, so a nested loop does around 5 billion comparisons in the worst case. That will likely time out on larger hidden tests. Use the hash map for O(n) time.
What should I return when no pair exists?+
Return [-1, -1]. Example 2 shows it: nums = [1,2,3] with target 7 gives [-1,-1]. Put that return after the loop ends. It's the line people forget when they rush from memory of the classic version.
How do I prepare for this in 48 hours?+
Write the one-pass hash map solution from scratch three times, then test it on duplicates, negatives, and a no-answer case. Also check overflow in your language of choice. Spend the rest of your time on medium-level array and hash problems, since the real OA likely has more than this one.