1. Two Sum
easyAsked at BroadcomFind two indices whose values add to a target. Broadcom screens for this in early rounds to verify you default to a hash-map solution over a brute-force nested loop — a signal that you instinctively reason about time complexity for large-scale data pipelines.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Broadcom loops.
- Glassdoor (2026-Q1)— Reported in Broadcom SWE phone-screen summaries as a warm-up hash-map problem.
- Blind (2025-10)— Multiple Broadcom threads cite Two Sum as an opening question in infrastructure software roles.
Problem
Given an array of integers nums and an integer target, return the indices of the two numbers such that they add up to target. You may assume that each input has exactly one solution, and you may not use the same element twice. Return the answer in any order.
Constraints
2 <= nums.length <= 10^4−10^9 <= nums[i] <= 10^9−10^9 <= target <= 10^9Only one valid answer exists.
Examples
Example 1
nums = [2,7,11,15], target = 9[0,1]Explanation: nums[0] + nums[1] = 2 + 7 = 9.
Example 2
nums = [3,2,4], target = 6[1,2]Explanation: nums[1] + nums[2] = 2 + 4 = 6.
Approaches
1. Brute force (nested loops)
Check every pair (i, j) where i < j. Return indices when nums[i] + nums[j] === target.
- Time
- O(n²)
- Space
- O(1)
function twoSum(nums, target) {
for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] === target) return [i, j];
}
}
return [];
}Tradeoff: Simple but O(n²). Unacceptable for large packet-lookup tables or flow tables common in Broadcom's networking firmware.
2. Hash map (one pass)
Walk the array once. For each element, check whether target − nums[i] already exists in the map. If yes, return the pair; otherwise store nums[i] → i.
- Time
- O(n)
- Space
- O(n)
function twoSum(nums, target) {
const seen = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) return [seen.get(complement), i];
seen.set(nums[i], i);
}
return [];
}Tradeoff: O(n) time with one pass. This is the expected answer at Broadcom — shows you recognise that O(n²) is unacceptable when searching multi-million-entry forwarding tables.
Broadcom-specific tips
Broadcom interviewers for infrastructure software roles will often ask: 'How does this scale to 100 million entries?' Lead with the hash-map approach and mention that O(n) lookup is critical for ASIC forwarding-table searches or flow-rule matching in network drivers. State the space trade-off explicitly — trading memory for speed is a core design philosophy in Broadcom's chip firmware.
Common mistakes
- Starting with the brute-force and never pivoting — name the O(n²) approach as a baseline, then immediately move to the hash-map.
- Returning values instead of indices — read the problem statement carefully.
- Not handling negative numbers — complement = target − nums[i] works for all integers.
- Assuming nums is sorted — the problem makes no such guarantee; sorting then two-pointer changes indices.
Follow-up questions
An interviewer at Broadcom may pivot to one of these next:
- What if the array is sorted? (Two-pointer approach becomes O(1) space.)
- What if you need all pairs, not just one?
- How would you handle this in a streaming setting where elements arrive one at a time?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Can I sort the array first and use two pointers?
Yes, but sorting changes the original indices, so you'd need to track original positions. The hash-map approach is cleaner when indices matter.
What if there are duplicate values in nums?
The map stores the most recently seen index. Since the problem guarantees exactly one solution, duplicates won't cause correctness issues, but you should note it.
Why does Broadcom ask Two Sum for infrastructure roles?
It tests whether you default to O(n) hash-map lookups — the mental model needed for efficient packet classification and forwarding-table design.
Practice these live with InterviewChamp.AI
Drill Two Sum and other Broadcom interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →