liveBook · Manning

Chapter 5: Hash tables

What you will learn in chapter 5 of Grokking Algorithms, Second Edition

The grocery-store price lookup example compares search speeds and motivates the need for hash tables.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The grocery-store price lookup example compares search speeds and motivates the need for hash tables.

Key terms: simple search, binary search, O(n), O(log n), O(1), hash functions

5.1 Hash functions

The section explains how a hash function and an array form a hash table that stores and retrieves values by key, while noting that real mappings are rarely perfect.

Key terms: hash function, strings, indexes, hash table, array, keys, values, perfect hash function, one-to-one mapping, collisions

5.2 Use cases

5.3 Using hash tables for lookups

The section shows how hash tables support lookups by mapping a key to a value, using a phone book and DNS as examples.

Key terms: hash table, mapping, key, lookup, DNS resolution, IP address, DNS cache

5.4 Preventing duplicate entries

The section shows how a hash table can track who has voted, prevent repeat votes, and avoid repeatedly scanning a long list.

Key terms: hash, hash table, voted, list, simple search, duplicates

5.5 Using hash tables as a cache

This section explains how caching reuses previously prepared web pages and how a hash table can store them by URL.

Key terms: caching, login page, hash, cache, page URL, page data

5.6 Recap

5.7 Collisions

The section explains what hash-table collisions are, how a linked list can handle them, and why unevenly distributed keys slow lookups.

Key terms: collision, keys, slot, hash function, linked list, collisions, linked lists

5.8 Performance

This section explains why hash tables are fast on average, how their worst-case performance differs, and why avoiding collisions matters.

Key terms: average case, O(1), constant time, worst case, O(n), collisions, load factor, hash function

5.9 Load factor

This section defines hash-table load factor, explains when and how to resize a table, and states the performance trade-off.

Key terms: load factor, slots, resizing, hash function, collisions, O(1)

5.10 A good hash function

The section explains that a good hash function spreads keys across a hash table and asks readers to judge distributions for different sets of strings.

Key terms: hash function, distribution, collisions, slot, hash functions, good distribution, index, keys

5.11 Recap

Recaps how hash tables are built, why they are fast, and what they are useful for.

Key terms: hash function, array, collisions, load factor, resize, average-case performance, constant time, search, insert, delete

Where this is used later

Concepts explained in this chapter

← Chapter 4: Quicksort Chapter 6: Breadth-first search →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.