Skip to main content

1. Two Sum

easyAsked at IBM

Two Sum is IBM's universal warm-up across SWE and intern phone screens. The interviewer is grading whether you can pivot from the obvious O(n^2) double loop to the one-pass hash-map solution and whether you can articulate the space-for-time tradeoff cleanly.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in IBM loops.

  • Glassdoor (2026-Q1)Appears in IBM SWE-1 and intern phone-screen recap threads.
  • LeetCode (2026-Q1)Top-10 IBM-tagged problem.
  • GeeksforGeeks (2025-11)Cited in IBM interview-experience archive.

Problem

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice.

Constraints

  • 2 <= nums.length <= 10^4
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9
  • Only one valid answer exists.

Examples

Example 1

Input
nums = [2,7,11,15], target = 9
Output
[0,1]

Example 2

Input
nums = [3,2,4], target = 6
Output
[1,2]

Example 3

Input
nums = [3,3], target = 6
Output
[0,1]

Approaches

1. Brute-force nested loops

Try every (i, j) pair with i < j and return when nums[i] + nums[j] == target.

Time
O(n^2)
Space
O(1)
function twoSumBrute(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: Constant extra space but n^2 time. At the upper-bound n=10^4 it still finishes in milliseconds, but leaving it on the board without naming the optimal earns a 'mixed signal' note.

2. Hash map one-pass (optimal)

Store each value's index in a Map as you scan. For each new num, look up target - num.

Time
O(n)
Space
O(n)
function twoSum(nums, target) {
  const seen = new Map();
  for (let i = 0; i < nums.length; i++) {
    const need = target - nums[i];
    if (seen.has(need)) return [seen.get(need), i];
    seen.set(nums[i], i);
  }
  return [];
}

Tradeoff: O(n) time at O(n) extra space — the canonical space-for-time trade. One pass works because by the time you'd want to pair X with Y, X is already in the Map.

IBM-specific tips

IBM rewards the explicit brute-force → optimal narration on this problem. State 'I'll trade space for time with a hash map' BEFORE coding the optimal — interviewers grade reasoning as much as the final answer. Skipping straight to the Map version without naming the trade reads as 'memorized the solution.' Watch out for the duplicate-value test case (nums = [3,3]) — confirm aloud that you handle it.

Common mistakes

  • Returning the values instead of the indices.
  • Writing the hash map as two passes (build then lookup) when one pass is cleaner.
  • Forgetting that duplicate values are allowed (nums = [3,3] with target 6 must return [0,1]).
  • Using `seen[need]` syntax with an Object — fails for numeric -0 vs 0 and is slower than a Map for large inputs.

Follow-up questions

An interviewer at IBM may pivot to one of these next:

  • What if the array were sorted? (Two-pointer in O(1) extra space.)
  • What if we needed all pairs that sum to target, not just one? (Frequency map + duplicate handling.)
  • Three Sum (LeetCode 15) — extend to triples.
  • What if the target is replaced with a range [lo, hi]?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Does IBM still ask Two Sum in 2026?

Yes, primarily as a 10-minute warm-up before the harder follow-up. Public IBM phone-screen reports list it as the most common opener. Expect the interviewer to pivot to Three Sum or the sorted-input variant if you breeze through it.

Is the one-pass hash map actually expected over two-pass?

Both pass the rubric on correctness, but one-pass is the canonical answer because it demonstrates the invariant: when you reach index i, the Map contains every prior index. The pair {earlier-half, later-half = i} has its earlier half already stored. State that invariant when you write the code.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

Drill Two Sum and other IBM interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →