Crafting Interpreters · Week 18
Ch 20: Hash Tables
Updated
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.
See it
All visualsStep through at your own pace; the arrow keys work too.
- 0empty
- 1kiwihome
- 2empty
- 3empty
- 4empty
- 5tombstone
- 6fighome 5
- 7applehome
Slot 6 holds "fig": found, because probing went on past the tombstone at slot 5.
This week's steps
4 stepsImplement, then write a small stress test that inserts and deletes many keys and checks lookups.
Write the code in your lox repo
Type it into clox/ (C) yourself as you read, and get it running.
Challenge 1 (support non-string keys) and challenge 2 (tune and compare hash table designs) are great; challenge 3 (benchmarking) pairs well with 2.
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 youSign 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 postsQuestions 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.