High interview relevanceRoadmap: Beginner
TimeLookup, insert, delete O(1) expected; O(n) worst case
SpaceO(n)

Why it costs this much

The hash function jumps straight to a bucket, so a lookup does not scan other keys. Resizing keeps buckets short, which keeps the expected cost constant.

Code

seen = set()
for x in items:
    if x in seen:       # expected O(1): hash, then check one short bucket
        return True
    seen.add(x)         # expected O(1)
return False            # whole loop: expected O(n)

Python is the baseline example used across the site.

Common pitfalls

Many colliding keys push a bucket toward O(n). Keys must be hashable and immutable. Iteration order and memory overhead differ between languages.

Operation by operation

OperationBestAverageWorstSpace
Hash table lookup, insert, deleteO(1)O(1) expectedO(n)O(n)

Hash table lookup, insert, delete. A hash function that spreads keys evenly, and a bounded load factor. Resizing makes one insert O(n), amortized O(1). Adversarial keys can force collisions. Iteration order is not sorted.

Keyboard shortcuts

/
Search topics
?
Show this list of shortcuts
r
Open the revision sheet
b
Save or unsave the focused card, or the topic you are reading
l
Mark the focused card, or the topic you are reading, as learned
Space Left arrow Right arrow
In the visualizer: play or pause, step back, step forward
1 to 4 Enter
In the quiz: pick an answer, then go to the next question
Escape
Close this list