Skip to content
Book a Call → mycocoon.life
← Back to Blog Students 11 min read

CS50 Week 5 Explained: Linked Lists, Hash Tables and Tries

Week 5 takes the pointers from Week 4 and uses them to build structures that arrays can't manage. Every structure here makes a trade-off between speed, memory and simplicity. By the end you'll understand what sits behind a Python dictionary or a JavaScript object, and you'll build a spell checker that races against everyone else's.

🎓
An unofficial study guide. CS50 is Harvard University's Introduction to Computer Science, taught by David J. Malan. The lectures, notes and problem sets linked here belong to Harvard and are shared under CC BY-NC-SA 4.0. This guide is Cocoon's own writing and is not affiliated with or endorsed by Harvard. Take the course itself, free, at cs50.harvard.edu/x. Part of our CS50 study series. Keep track of your progress with our free CS50 Study Tracker.
CS50 Week 5 Explained: Linked Lists, Hash Tables and Tries, pixel-art illustration
CS50 Week 5 Explained: Linked Lists, Hash Tables and Tries
Thumbnail of CS50x 2026, Lecture 5: Data Structures
Watch the lecture

CS50x 2026, Lecture 5: Data Structures

2 h 4 min · Harvard University · CS50 on YouTube

Jump to the key moments

  1. 0:43Jack Learns the Facts
  2. 3:35Stacks and queues
  3. 12:26Resizing arrays
  4. 27:16realloc
  5. 33:03Linked lists
  6. 1:22:04Trees
  7. 1:36:47Hashing and hash tables
  8. 1:53:51Tries

Abstract data types: stacks and queues

Some structures are defined by how they behave rather than how they're built. A queue is first in, first out, like a line at a shop. A stack is last in, first out, like a pile of trays (or your email inbox, if you always read the newest first). You can build either on top of an array or a linked list.

Why arrays get awkward

An array has a fixed size. To add one more item, you often have to allocate a bigger block, copy everything across and free the old one, which is O(n) work every time it grows. realloc tidies up the code but not the underlying cost. That's the problem the rest of the lecture solves.

Linked lists

A linked list is a chain of nodes. Each node holds a value and a pointer to the next node, and the last one points to NULL. Adding at the front is O(1): make a node and point it at the old first node. The cost is that finding an item is back to O(n), because you can't jump to the middle as you can with binary search. Each node also uses extra memory for its pointer.

Trees

A binary search tree gives each node two pointers: smaller values go left and larger values go right. Searching halves the options at every step, so it's O(log n), as long as the tree stays balanced. Insert sorted data into a naive tree and it degrades into a long, thin linked list.

Hash tables

A hash table is an array of "buckets", where each bucket is a linked list. A hash function turns a key (say, a word) into a bucket number, so you look in one small bucket instead of the whole collection. With a good hash function and enough buckets, lookups are close to O(1) on average. With a bad one, everything lands in the same bucket and you're back to a linked list.

Tries

A trie is a tree where each level is one letter of a word. Looking up a word takes as many steps as the word has letters, however many words are stored: constant time in practice. The trade-off is memory, because every node carries a slot for every possible letter.

Problem Set 5: what it asks

Where people get stuck

Check yourself

What's the running time of searching an unsorted linked list, and why can't you use binary search on it?

O(n). Binary search needs to jump straight to the middle, and a linked list can only be walked from the front.

What makes a hash function "good" for Speller?

It spreads words evenly across buckets, it always gives the same bucket for the same word, and it's quick to compute.

When would you pick an array over a linked list?

When the size is fairly stable and you need fast access by index or binary search. Arrays also use less memory per item.

What's the trade-off with a trie?

Near-constant lookup time, paid for with a lot of memory, because each node reserves space for every possible next letter.

Hash tables and tries also power search in AI systems. See what retrieval looks like from the inside in the RAG Lab.

Explore the RAG Lab →