hash_table.state
Input
Hash table
Current look-up
Match / window
Stored entry
What is Two Sum
For each number x ask whether target − x was already stored. The hash map remembers value → index, so the complement check is one probe — not a second loop.
x is uniquely target − x.2, 7, 11, 15 with target 9. 2 is stored, then 7 finds complement 2 and returns [0, 1].Interactive trace
Pseudocode & Complexity
| Operation | Time |
|---|---|
| Complement lookup | O(1) expected |
| Full scan | O(n) |
| Space | O(n) |
Python implementation