iq.lab
Python starts when a code cell comes near or you run one

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.

  1. 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
  2. 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
  3. 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
  4. 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.

Next moduleGraphs