Two-Sum With a Hash Set

Coding
Hashing
Easy
What is the time complexity of finding whether a target pair summing to a given value exists using a hash set?
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. 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. 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

Related problems