Search HoldMyTwist

Browse all posts

Episode 43

The Two Ducks in Drawer One

Duck opens the drawer labelled "one". Inside are tea and boots. Count the letters in a name by threes. Whatever's left over tells Duck which drawer to open.

Episodes

Computing Basics

    Duck shows us his 3-drawer cabinet in the monitor. Robot is pointing at the rule for number of letters.
    DUCK. So it's got 4 letter tiles. Rule says divide that by 3. Get the remainder (1). Use that as drawer one. DUCK and BOOTS go together.
    Duck has an alternate entry named MOON in addition to the existing DUCK entry. They're both 4 letters.
    Robot maintains DUCK and MOON in separate entries, but linked in the same drawer.
    Duck is searching for MOON. In bucket 1. DUCK-BOOTS in the chain. MOON-TEA in the chain.
    The lookup pointer tests DUCK and MOON and finds they are not equal. The lookup pointer moves on to the next entry.
    The pointer lands on MOON, and gets the tea value. The boots stay with DUCK.
    It first tries to look into bucket 1. It rejects DUCK and finds MOON. It gives TEA and DUCK holds BOOTS.
    Duck adds FROG lookup for same drawer. No FROG entry exists.
    The pointer declines DUCK & MOON. It then goes to the end of the chain.
    Duck gets not found. Saved values are not modified. Two saved values are there.
    DUCK is still BOOTS. MOON is still TEA. There is a card that suggests changing DUCK to HAT. But that suggested change has not happened yet, it's a separate thing.
    Only CAT and HEN keys visible in practice chain. Values are covered by blank covers.
    The practice lookup first rejects the CAT and then finds the HEN.
    HEN lookup = SEEDS, CAT still in the same bucket with CAKE.
    Robot points at the full records, two of them in the same drawer. They still have key-value pairs. Duck pulls the bell next to the drawer.

    Episode transcript

    One crowded drawer

    Duck shows us his 3-drawer cabinet in the monitor. Robot is pointing at the rule for number of letters.

    Diagram values: h(KEY) = LETTER COUNT mod 3; 0; 1; 2; DUCK = BOOTS; MOON = TEA

    Duck: I use all those names to select a numbered drawer.

    DUCK. So it's got 4 letter tiles. Rule says divide that by 3. Get the remainder (1). Use that as drawer one. DUCK and BOOTS go together.

    Diagram values: DUCK: 4 mod 3 = 1

    Robot: Four letters. 1 left over. Drawer 1.

    Duck has an alternate entry named MOON in addition to the existing DUCK entry. They're both 4 letters.

    Diagram values: MOON: 4 mod 3 = 1

    Duck: MOON has four letters. Will my tea turn into boots?

    Robot maintains DUCK and MOON in separate entries, but linked in the same drawer.

    Diagram values: 1: DUCK=BOOTS -> MOON=TEA

    Robot: Keep both different keys in one bucket.

    Compare the whole key

    Duck is searching for MOON. In bucket 1. DUCK-BOOTS in the chain. MOON-TEA in the chain.

    Diagram values: LOOK UP MOON; BUCKET 1

    Duck: Hey, Robot! Fetch me the tea value with the key MOON!

    The lookup pointer tests DUCK and MOON and finds they are not equal. The lookup pointer moves on to the next entry.

    Diagram values: DUCK != MOON; NO MATCH

    Robot: Compare the entire key. MOON is not DUCK.

    The pointer lands on MOON, and gets the tea value. The boots stay with DUCK.

    Diagram values: MOON = MOON; RETURN TEA

    Duck: MOON matches! Check the tea!

    It first tries to look into bucket 1. It rejects DUCK and finds MOON. It gives TEA and DUCK holds BOOTS.

    Diagram values: HASH -> BUCKET; EQUALITY -> ENTRY

    Robot: The hash selects the drawer, we still compare the full names.

    A visitor with no entry

    Duck adds FROG lookup for same drawer. No FROG entry exists.

    Diagram values: FROG: 4 mod 3 = 1

    Duck: What if I asked for FROG? 4 letters again.

    The pointer declines DUCK & MOON. It then goes to the end of the chain.

    Diagram values: DUCK != FROG; MOON != FROG; END

    Robot: Neither key equals FROG!

    Duck gets not found. Saved values are not modified. Two saved values are there.

    Diagram values: FROG: NOT FOUND

    Duck: Same drawer, nothing.

    DUCK is still BOOTS. MOON is still TEA. There is a card that suggests changing DUCK to HAT. But that suggested change has not happened yet, it's a separate thing.

    Diagram values: PROPOSED: DUCK=HAT; CURRENT: DUCK=BOOTS

    Robot: No update is made.

    A second little cabinet

    Only CAT and HEN keys visible in practice chain. Values are covered by blank covers.

    Diagram values: PRACTICE; CHAIN CAT -> HEN; LOOK UP HEN; VALUES COVERED

    Duck: What key will our HEN lookup reject first?

    The practice lookup first rejects the CAT and then finds the HEN.

    Diagram values: CAT != HEN; HEN = HEN

    Robot: Both hashes are zero.

    HEN lookup = SEEDS, CAT still in the same bucket with CAKE.

    Diagram values: RETURN SEEDS; CAT=CAKE UNCHANGED

    Duck: Seeds for HEN.

    Robot points at the full records, two of them in the same drawer. They still have key-value pairs. Duck pulls the bell next to the drawer.

    Diagram values: TOY HASH; SEPARATE CHAINING

    Robot: We distinguish between entries by full names.

    The lesson

    We count the number of letters in an uppercase ascii key, divide that by 3, and take the remainder. The remainder is the bucket. For example, if key has 4 letters, the remainder is 1. So the key DUCK and the key MOON will hash to bucket 1. Collisions occur when different keys hash to same bucket. For separate chaining, all the full key-value entries remain chained in the same bucket. When we want to find MOON we will check the entire key in DUCK and then in MOON. DUCK key doesn't match. We get TEA on the MOON key check. If you look up the value for key FROG, you will find none at the end. The values in storage do not change when you read. You may change the value for full key that matches if you separately do a put operation. We will not be doing that update DUCK=HAT. So DUCK still has value BOOTS. In this toy example, new keys will be added to the end of the chain. Other implementations may put them in other order. This toy "hash" with letter count is not a cryptographic hash. It is a teaching example.

    Try it

    You have three empty buckets. Use the same letter count modulo 3 hash. In this toy table, the new unique keys get added to the end of the chain for the bucket. Add SUN=HAT then PIG=PIE. Look up DOG. Which bucket does DOG go to? Do you check DOG against SUN or PIG first? What's the final result? Did either of the saved values change?

    Answer

    3 letters. Divide 3 by 3. No remainder. So DOG picks bucket 0. The chain starts with SUN and then PIG. DOG compares to SUN and then to PIG. Neither one is it. Not found. Now we have SUN->HAT and PIG->PIE.

    Sources

    Princeton Algorithms authors: SeparateChainingHashST source (opens in a new tab)