Crafting Interpreters · Week 18

Ch 20: Hash Tables

Updated

About 7 hoursVerified by tests

Summary

Crafting Interpreters chapter 20, Hash Tables: A hash table from scratch with open addressing and linear probing, FNV-1a hashing, tombstones for deletion, growth by load factor, and string interning so string equality becomes pointer equality. Budget about 7 hours; the book's chap20_hash tests check your code.

Flashcards 10 cards for Anki and AnkiMobile · all decks

Anki deck.tsv

Step through at your own pace; the arrow keys work too.

Crafting Interpreters · Ch 20 · TAOCP 6.4 A hash table with linear probing Collisions probe to the next slot, deletions leave tombstones, and a full enough table doubles. Type your own keys.

find figcount 4live 3capacity 8load 0.50

  1. 0empty
  2. 1kiwihome
  3. 2empty
  4. 3empty
  5. 4empty
  6. 5tombstone
  7. 6fighome 5
  8. 7applehome

Slot 6 holds "fig": found, because probing went on past the tombstone at slot 5.

This week's steps

4 steps
  1. Read the chapter

    Implement, then write a small stress test that inserts and deletes many keys and checks lookups.

  2. Write the code in your lox repo

    Type it into clox/ (C) yourself as you read, and get it running.

  3. Do the challenges (1–3)

    Challenge 1 (support non-string keys) and challenge 2 (tune and compare hash table designs) are great; challenge 3 (benchmarking) pairs well with 2.

  4. Push and let the tests verify it

    Your repo runs the book's chap20_hash tests on every push; passing marks this done.

Sign in to track this: tick each step off and it counts toward your week.

Your notes

Only you

Sign in to keep private notes on this guide, in Markdown. They stay yours: never on your profile, and you can export them all.

Discussion

0 posts

Questions about this chapter, tips, and what tripped you up. Hints over answers: full solutions go in a spoiler.

No posts yet. Ask a question or share a tip.

Sign in to postAnyone can read. Posting needs an account with a confirmed email.

Reference

Why it matters

Hash tables are the most used data structure in practice. Building one yourself demystifies every dictionary and map you use, and Knuth 6.4 later analyzes exactly this design.

How to approach it

Implement, then write a small stress test that inserts and deletes many keys and checks lookups. Re-read the tombstone section twice; it is the subtle part.

Challenges

3 challenges at the end of the chapter.

Challenge 1 (support non-string keys) and challenge 2 (tune and compare hash table designs) are great; challenge 3 (benchmarking) pairs well with 2.

Common traps
  • Not treating tombstones correctly on insert versus lookup.
  • Infinite loop when the table has no empty slots (load factor too high).
  • Counting tombstones in the load factor or not, inconsistently.

Spot a mistake? Report it

Back to week 18