# How a hash table finds a value Intermediate

Elena Ruiz

Friendly · Curious · Supportive

11 min

Trace how a hash table uses a key to choose where to look, then checks for a match before returning the value.

## What is covered in this lesson

- Distinguish the requested key from a storage position.
- Recover the same hash code that storage used.
- Calculate the home position from the code and the bucket count.
- Trace the route from the home index to the first candidate.
- Identify the matching entry despite a shared home index.
- Return the matching value without changing the table.

## Questions this lesson answers

### [How can 5 find ash if there's no bucket 5?](<https://penplayground.com/seeds/explanation.2f99a105f033b35ca6fecdf58405823f34309c1488736517b8e9d4137e5d5662?lesson=les_bKNnNAxf00brIXoU&lang=en>)

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

### [Why does lookup give the same hash code?](<https://penplayground.com/seeds/explanation.cb63678afb7ab95f7eb4ac40f65abf93c769946a0584d47286241e5d1d38c3ad?lesson=les_bKNnNAxf00brIXoU&lang=en>)

Lookup gives H(5) = 5 again: 5 is the same hash code, not a table position.

### [How does a code choose its starting bucket?](<https://penplayground.com/seeds/explanation.d3b17b18a4a0639658cf91521348a12fe7f6bf14d74905c11c84dcadd7d2cc9d?lesson=les_bKNnNAxf00brIXoU&lang=en>)

When you divide 5 by 4, what's left is 5 − 4 = 1, so the home index is 1.

### [How do you reach the first pair to check?](<https://penplayground.com/seeds/explanation.19557e7999321fb4eda7fa1cf373c602c014443d61e8e5563933244b985b60c1?lesson=les_bKNnNAxf00brIXoU&lang=en>)

For key 5, read position 1 in the array and follow its reference to 1: oak, the first pair to check.

### [How do you find the right key when keys share a bucket?](<https://penplayground.com/seeds/explanation.15384bcf197b4566e554ce625f3ed6d28754bb203830fd19dd19913398f1c76b?lesson=les_bKNnNAxf00brIXoU&lang=en>)

After a mismatch, follow the next link until you find a match or reach the end.

### [What does looking up key 5 give you back?](<https://penplayground.com/seeds/explanation.1df90d2f917301ef397956a3a9d7649eafd0913391a1d52ad469a3166e94d7e0?lesson=les_bKNnNAxf00brIXoU&lang=en>)

Looking up key 5 gives you ash without changing either pair.

## Transcript

### A name, not a position

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

0:08 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.

0:46 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.

1:11 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.

### Why hash codes repeat

1:40 The storage key is 5. It goes into the rule now, and when you look it up later, you'll need to get the same number back.

1:49 Notice the storage key, 5, because once you know the key and the rule, the answer lookup needs is already fixed. Look at the storage line: this rule gives back exactly what went in, so the result is 5. A hash function is a rule that takes a key and gives you an integer. This particular hash function leaves your number unchanged. That 5 after the equals sign is the hash code, the integer you get back for the key. So the hash code for 5 at storage is 5. Giving you back the very number you put in is what makes this the identity rule. The identity rule works this way for every integer key. We write k for whichever key you put in. On the storage line, k is 5. Now H of k stands for the code you get back for that key. Here H of k equals k, so the storage line reads H of 5 equals 5.

2:57 Now suppose the key you're looking for is 5. Follow the lookup line: you apply the same identity rule, so H of 5 equals 5 again. You get matching codes because the rule depends only on the key, and whether you're storing or finding, the key hasn't changed.

3:16 Now look at the 4 table positions and read their numbers: 0, 1, 2 and 3. None of the table positions is numbered 5, but 5 is still the right hash code because it's what the rule gives back. So when you look up the same key, using the same rule gets you the same code, even if that code isn't a position in the table.

### Choosing a home index

3:42 The hash code slides into place above the buckets. It has to tell you which bucket to use, but none has that number.

3:51 The hash code is 5, so you need to turn it into a number you can find on this row. Look at the buckets numbered 0, 1, 2 and 3, and you'll see all 4 available choices. That count goes into the calculation as the number to divide by, so you divide 5 by 4. You're counting every bucket, even an empty one, because the capacity is the number of buckets the table has. With a capacity of 4, you take away whole groups of 4 and keep what's left.

4:25 Now count what's stored instead: there are just 2 entries, and that count is the table's size. Size tells you how much is stored, not how many places there are to put it. If you divided by 2 instead, your only available choices would be buckets 0 and 1. That's because 2 left over would already make another pair, so no code could ever land in bucket 2 or 3 that way.

4:54 So go back to all 4 buckets and use that count to make whole groups of 4. Just 1 group of 4 fits into 5, because 2 groups would take 8. So subtract the whole group that fits: 5 minus 4 leaves 1. What's left after you take away as many whole groups as fit without going below zero is the remainder. Here the remainder is the 1 you keep, not the group you took away.

5:25 Notice why the calculation always leaves less than 4: if there were any more than 3 left, you could take away another whole group. Now try the small codes, 0, 1, 2 or 3, and each leaves itself because no whole group of 4 fits. So those are all the possible leftovers, and they match the numbers on the buckets exactly.

5:49 Follow the result for hash code 5: the 1 left over goes straight to bucket 1 because the numbers match. That first bucket number the table chooses for your key is its home index. So dividing 5 by the bucket count leaves 1, and that makes 1 your home index.

### Finding the first pair to check

6:10 When you look up key 5, you go straight to position 1. What's stored there tells you where to start looking for a pair.

6:19 See position 1 here? That's the home index for key 5, the position assigned to that key. So you read position 1 directly in the array and skip the other 3 positions. This spot is a bucket because it's where you look for all the pairs that share a home index. Every pair with home index 1 belongs to this bucket. What you read here tells you how to reach a pair stored outside this spot. That value is a reference because it points you to a particular pair you can reach. See how the arrow starts inside the cell, right where the reference is stored.

6:59 Now suppose no pairs belonged to bucket 1: you'd still read the same cell, but it'd be empty. With no first pair to point to, that bucket would have no reference for you to follow.

7:11 Back in our table, bucket 1 does hold a reference to its first pair. Follow that reference and you reach the oak pair, with key 1 and value oak. The pairs in this bucket are linked in order, so the next link takes you from the oak pair to the ash pair. Together those linked pairs form a chain that starts right where the reference points. So the oak pair comes first in this chain. That means when you look up key 5, you read only position 1 in the array and follow its reference to the oak pair as your first pair to check.

### Testing keys in order

7:47 You're looking for 5, so you start at the oak entry. This chain holds 2 keys, and you need to tell them apart.

7:56 Bucket 1 narrows down where you need to look, but both keys belong here. In this table of 4 buckets, each key is its own hash code, the number you use to work out which bucket to look in. So the hash code for the oak entry is 1, and the hash code for the ash entry is 5. Look at how many groups of 4 fit: 1 is 0 groups of 4 plus 1, and 5 is 1 group of 4 plus 1. It's the leftover number that tells you where to look, so that last 1 puts both keys in bucket 1. When different keys land in the same bucket like this, you've got a collision. Notice that this collision happens even though the codes are different.

8:43 Now put the bucket numbers aside and compare the 1 in the oak entry with the 5 you're looking for. Since 1 isn't equal to 5, skip the oak entry and follow the next link to the ash entry. Sharing a bucket isn't enough because what matters is key equality, the rule for deciding whether the stored key and the key you want count as the same key. For these keys, key equality means the numbers themselves must be equal.

9:13 Now look at the ash entry: 5 equals 5, so you've got a match. You can stop here because this pair has the very key you asked for.

9:23 But suppose the ash entry weren't there and the oak entry had a link to the end instead, with an end marker telling you there's nothing left to test. Then 5 would be missing because it would have to be in bucket 1, and you've checked every key there. So start with the bucket it belongs in and follow the links in order after each mismatch until you find the key or reach the end without it.

### Returning the matching value

9:47 Watch the pointer move from the key to the value beside it. You gave the lookup a key because you want that value back.

9:56 The pointer is at ash because the second pair has the key you asked for, 5, right beside ash. Now the outline marks the result cell, but it's still blank because the lookup hasn't given you anything back yet. The answer an operation gives back to whoever asked is its return value. So the result cell is for the return value, not for another stored pair.

10:22 Now follow the pointer to the first pair and notice whose value it holds: it belongs to key 1. The keys share a bucket, but each has its own pair, so taking the first value would answer for 1 instead of 5.

10:38 Now follow the pointer back to the second pair: the lookup reads ash there, so the blank result cell fills with ash. Both pairs stay just as they were and right where they were, because reading a value doesn't take it out of the table. So when you ask for key 5, you get ash because that's the value paired with 5, whether the match comes first or not.
