3/40
Part 1 of 7 · Sets and seen-setsLearn

The hash picks a bucket, so a lookup is O(1) on average

How can a set skip the scan? Before it stores a key, it turns the key into a number, and the number says where the key goes.

keys = [12, 33, 7, 42]
print([hash(k) for k in keys])
print([hash(k) % 8 for k in keys])
Predict

For a small int, hash(k) is k. A table of 8 buckets files a key under hash(k) % 8. Which bucket does 42 go in?

Pick one to see what it prints.
Pick an answer above to see the rest
Predict

To check whether 33 is in the table, how many of the other keys does the lookup compare it with?

Pick an answer above to see the rest