Skip to main content

1. Two Sum

easyAsked at Google

Two Sum is Google's canonical warm-up: given an integer array and a target, return the indices of the two numbers that add up to the target. The interviewer is grading your willingness to narrate the space-for-time tradeoff before jumping to the optimal.

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

Source citations

Public interview reports confirming this problem appears in Google loops.

  • Glassdoor (2026-Q1)Appears in Google SWE-new-grad phone-screen reports as a warm-up before a harder follow-up.
  • Reddit r/cscareerquestions (2025-11)Multiple Google L3 reports list Two Sum as the first 10-minute warm-up.

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. You can return the answer in any order.

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]

Explanation: nums[0] + nums[1] == 9, so we return [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: Simplest to reason about and zero extra space; at the constraint upper bound (10^4) n^2 still runs in well under a second but it's the wrong answer for any tier-1 interviewer to leave on the board without naming the optimal.

2. Hash map (optimal, one-pass)

Store each value's index in a hash 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 tradeoff. One-pass works because by the time you'd want to pair X with Y, you've already inserted whichever appeared first.

Google-specific tips

Google interviewers specifically value the brute-force → optimal narration on this one. Talk through the O(n^2) version first, name the cost in plain language, then say 'I can trade space for time with a hash map' and write the one-pass solution. Jumping straight to the optimal is a flag because Google's rubric explicitly tests whether you can articulate complexity tradeoffs, not just produce working code.

Common mistakes

  • Returning the values instead of the indices.
  • Using a hash map but writing it as two passes (build then lookup) when a one-pass is cleaner.
  • Forgetting that duplicate values are allowed (nums = [3,3] with target 6 must return [0,1]).

Follow-up questions

An interviewer at Google 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.)
  • What if we extended to Three Sum? (Sort + fix one + two-pointer the rest, O(n^2).)

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 Google still ask Two Sum in 2026?

Yes, but rarely as the final question. It's a 10-minute warm-up to confirm you can code at all before the harder follow-up. Expect the interviewer to pivot to Three Sum or the sorted-array variant within 15 minutes.

Is the one-pass hash map version actually expected, or is two-pass acceptable?

Both pass the rubric on correctness, but the one-pass is the canonical answer because it demonstrates you understand the invariant: when you arrive at index i, the map contains every prior index, so any pair whose later half is i has its earlier half already stored.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →