Module 9 · 3 lessons · about 5 hours 40 minutes
Heaps and tries
How a heap keeps the smallest item on top in an ordinary list, top-k and merging with heapq, the two-heap median, and prefix trees for words.
- Lessons
- 0/3
- Exercises
- 0/24
- Code
- 0/3
- Problems
- 0/11
- Checkpoint
- Not taken
Already comfortable with this? Take the checkpoint cold. If you pass, move on and come back only for what you missed.
- 1How a heap worksA heap is a list read as a tree, kept in an order where the smallest item always sits at index 0. Push and pop each walk one path of that tree, so both cost O(log n), and Python's heapq does the walking for you.1 h 35 min
- 2Top k and mergingTo find the k largest of n numbers, hold only k of them in a min-heap and let the smallest leave. That costs O(n log k). A heap of the k fronts merges k sorted lists, and two heaps facing each other keep the median of a stream.2 h 30 min
- 3TriesA trie stores words as paths of letters that hang from one root, so words that start alike share their first nodes. Insert, search and starts-with each walk one letter per step, and a wildcard search tries every branch, depth first.1 h 35 min
- Checkpoint12 questions. Pass with 80% to finish the module.
Problems for this module
The lessons teach some of these step by step (each says which). The rest are practice: try each in its target time, and solved problems come back for spaced re-solves.
- easyLast stone weight15 min
- easyKth largest in a stream15 min
- mediumKth largest number25 min
- mediumK closest points to the origin25 min
- hardMerge k sorted lists40 min
- hardMedian of a stream40 min
- mediumJobs with a cooldown25 min
- mediumMost searched words25 min
- mediumBuild a prefix tree25 min
- mediumWord store with wildcards25 min
- hardFind every word on a letter grid40 min