bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch
🌱Newbie
0 XP
Back to Two Sum
🐛Spot the BugpythonEasyArrays & Hashing

Two Sum - Spot the Bug (Python)

Find the bug in Python

Problem Brief

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.

Puzzle Hints
  1. Test the line against the actual promise of Two Sum, not just whether the syntax looks valid.

  2. The repair changes only this statement shape: "seen[i] = num". Keep the same role, but make it match the invariant.

  3. Storing i→num instead of num→i. We need to look up by value, not index.

Asked at 72 companies
AdobeAetionAffirm
Two Sum — HashMap Lookup
hashmap
nums271115
1
5

Input: [2,7,11,15], target=9. Use HashMap: complement → index

dict & set in Pythonref

Python dict is a hash map with O(1) average access. set stores unique hashable elements. Both are built-in and highly optimized. Use collections.Counter for frequency counting and collections.defaultdict to avoid KeyError.

-dict — {key: value}, O(1) access via d[key]
-set() — unique elements, O(1) in/add/discard
-dict.get(key, default) avoids KeyError
-Counter(iterable) counts element frequencies
from collections import Counter, defaultdict

d = {}
d['a'] = 1
d.get('b', 0)       # 0 (default)

s = set([1, 2, 2])  # {1, 2}
2 in s               # True — O(1)

Counter("aab")       # Counter({'a': 2, 'b': 1})
Official docs →
dict & set in Pythonref

Python dict is a hash map with O(1) average access. set stores unique hashable elements. Both are built-in and highly optimized. Use collections.Counter for frequency counting and collections.defaultdict to avoid KeyError.

-dict — {key: value}, O(1) access via d[key]
-set() — unique elements, O(1) in/add/discard
-dict.get(key, default) avoids KeyError
-Counter(iterable) counts element frequencies
from collections import Counter, defaultdict

d = {}
d['a'] = 1
d.get('b', 0)       # 0 (default)

s = set([1, 2, 2])  # {1, 2}
2 in s               # True — O(1)

Counter("aab")       # Counter({'a': 2, 'b': 1})
Official docs →
How to think: Hash Map / Setguide

You need O(1) lookups — checking if something exists, counting frequencies, or finding pairs.

1.Ask: "Am I searching for something repeatedly?" → Hash Map
2.Ask: "Do I need to check existence?" → Set
3.Ask: "Do I need to count occurrences?" → Map with value = count
4.Ask: "Do I need to find a pair that satisfies a condition?" → Store complement in Map
5.The tradeoff: O(n) extra space buys you O(1) per lookup

vs Nested loops (O(n²)): You're comparing every element against every other — a Map does it in one pass

vs Sorting (O(n log n)): You just need existence/frequency checks, not order

find pairtwo numbers thatfrequencycountduplicateanagramgroup by
How to think: Hash Map / Setguide

You need O(1) lookups — checking if something exists, counting frequencies, or finding pairs.

1.Ask: "Am I searching for something repeatedly?" → Hash Map
2.Ask: "Do I need to check existence?" → Set
3.Ask: "Do I need to count occurrences?" → Map with value = count
4.Ask: "Do I need to find a pair that satisfies a condition?" → Store complement in Map
5.The tradeoff: O(n) extra space buys you O(1) per lookup

vs Nested loops (O(n²)): You're comparing every element against every other — a Map does it in one pass

vs Sorting (O(n log n)): You just need existence/frequency checks, not order

find pairtwo numbers thatfrequencycountduplicateanagramgroup by
Inspect the code — click the line with the bug
1def twoSum(nums, target):
2 seen = {}
3 for i, num in enumerate(nums):
4 comp = target - num
5 if comp in seen:
6 return [seen[comp], i]
7 seen[i] = num