How can 5 find ash if there's no bucket 5?

A name, not a position

Elena Ruiz

How a hash table finds a value

To look up ash, give its key, 5, not the pair's position.

Transcript

Here comes a request carrying 5. As it slides beside the table, think about what that number asks the table to find.

To see what the request's 5 could mean, look first at the numbered places in the table. Count along with me: 1 place at position 0, then 2 at position 1, 3 at position 2, and 4 at position 3. Each of those places is a bucket where you start looking for any pairs that belong there. Even an empty bucket has a position number. Counting all those places gives you the table's capacity. So the capacity is 4 even though the last position is 3.

Now look at the stored pairs, because what counts here is a name together with its value. Count the first pair, 1 with oak, then the second pair, 5 with ash. That count gives you the table's size, which tells you how many pairs it holds. Its size is 2 because both pairs count even though they share a bucket.

Look back at the numbered places: you can't go to bucket 5 because they stop at 3. Instead, slide the request beside the second pair and you'll see that the stored name 5 goes with ash. That name is the key you give the table to ask for the value paired with it. So the request carrying key 5 asks for ash. That's why you can look up a value in a hash table by name without saying where the pair is stored.