Two-Sum With a Hash Set
Coding
Hashing
Easy
Hint 1
For each element, check whether its complement has already been seen.
Hint 2
Hash set insertions and lookups take expected time each.
Show worked solution
1. Describe the single-pass algorithm
Iterate through the array once, and for each element check whether is already in the hash set before inserting .
2. Account for hash set operations
Each of the iterations performs an expected lookup and insertion, giving expected total time.
Answer: Expected O(n)
Related roles: Browse quant jobs