Find the bug · Easy

Fix the Two Sum Hash Map

Easy Find the bug Most askedSeen at Amazon, Google, Meta

twoSum(nums, target) should return the indices [i, j] (i < j) of the two different elements that add up to target. Exactly one answer exists.

This one-pass hash-map version is the right idea, but it returns wrong answers for some inputs. Find the bug and fix it.

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]
Explanation: The buggy version returns [0, 0].

The buggy code

function twoSum(nums, target) {
  const seen = new Map(); // value -> index
  for (let i = 0; i < nums.length; i++) {
    seen.set(nums[i], i);
    const j = seen.get(target - nums[i]);
    if (j !== undefined) return [j, i];
  }
  return [];
}

Hints

Hint 1

Trace [3, 2, 4], target 6, step by step.

Hint 2

When you look up the partner, which elements should already be in the map?

Find and fix the bug

Loading the editor… (JavaScript is required to run code in your browser.)

Solution and explanation

Show the bug and the fix

Time O(n) · Space O(n)

The bug: the current number is inserted into the map before its partner is looked up. When target - x == x (for example 3 + 3 = 6), the lookup finds the element itself and returns [i, i]. With duplicates, the insert also overwrites the earlier index.

The fix: look up target - nums[i] first; only if it is missing, store nums[i]. The map then only ever contains elements before i, so an element can never pair with itself.

Fixed version

function twoSum(nums, target) {
  const seen = new Map(); // value -> index, only for elements before i
  for (let i = 0; i < nums.length; i++) {
    const j = seen.get(target - nums[i]);
    if (j !== undefined) return [j, i];
    seen.set(nums[i], i);
  }
  return [];
}

Common mistakes

  • Patching it with if (j !== i): works here, but the overwrite problem with duplicates remains.