Skip to main content

1. Two Sum

easyAsked at Gusto

Find the two indices whose values sum to a target. Gusto uses this as a warm-up to see if you think in hash maps before brute force — they care about naming, clean early returns, and whether you'd write a test for the no-solution case.

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

Source citations

Public interview reports confirming this problem appears in Gusto loops.

  • Glassdoor (2026-Q1)Reported as a phone-screen warm-up problem in multiple Gusto SWE interview reports.
  • Blind (2025-11)Gusto threads confirm Two Sum appears early in the process to assess baseline problem-solving and communication clarity.

Problem

Given an array of integers nums and an integer target, return the indices of the two numbers that 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^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] = 2 + 7 = 9.

Example 2

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

Explanation: nums[1] + nums[2] = 2 + 4 = 6.

Approaches

1. Brute force (O(n²))

Check every pair. Correct but too slow for large inputs.

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: Easy to write but O(n²) time. Use this only to establish correctness before optimizing.

2. Hash map (O(n))

Store each number's index in a map. For each element, check if the complement (target - num) already exists in the map.

Time
O(n)
Space
O(n)
function twoSum(nums, target) {
  const seen = new Map(); // value -> index
  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 []; // no solution (per constraints, this won't be reached)
}

Tradeoff: Single pass, O(n) time and space. This is the expected answer at Gusto. The interviewer may follow up asking how you'd test the empty-result path.

Gusto-specific tips

Gusto interviewers often pause mid-problem and ask 'how would you test this?' Name your variables clearly — `complement` beats `diff`. Walk through both a normal case and an edge case (e.g., negative numbers, duplicate values) before coding. They appreciate candidates who call out the single-solution assumption from the constraints rather than ignoring it.

Common mistakes

  • Using the same index twice — the problem states you can't reuse the same element.
  • Inserting the current number into the map before checking for its complement — this allows self-matching on a number that equals target/2.
  • Returning indices in the wrong order or forgetting to return anything on no-solution.
  • Jumping to the hash map solution without explaining the brute-force baseline first.

Follow-up questions

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

  • What if the array is sorted — can you do it in O(1) extra space? (Two-pointer approach.)
  • What if there can be multiple valid pairs? Return all of them.
  • How would you unit-test this function?

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Why store the value as key and index as value in the map?

You need to look up whether a specific value (the complement) has been seen. Keying by value makes that lookup O(1).

What if two elements have the same value?

The map stores the most recent index for a given value. But since the problem guarantees exactly one solution, duplicates that aren't part of the answer won't corrupt the result.

Does insertion order matter when building the map?

Yes — you must check before inserting so you don't match an element with itself when target is exactly 2× that element.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →