SKIP LISTS & TRIES
Two ways to search without a balanced tree
A skip list is a sorted linked list where coin flips let some nodes also join higher express lanes (levels), and search starts in the top lane. In a 32-key skip list, every node ends up at level 1 only. How many forward hops does searching for the largest key take?
A skip list is a sorted linked list with express lanes. Every node is on level 1. A coin decides how many more levels it joins: keep flipping while heads, so about half the nodes reach level 2, a quarter level 3. Search starts in the top lane, runs right until the next key is too big, then drops a level. Expected cost is O(log n), with no rotations.
A trie skips comparisons altogether. It walks the key one character at a time, so a lookup costs the key’s length, however many keys are stored.
function randomLevel(rng, p, max) {
return 1; // every node on level 1: a sorted linked list, O(n) search
}function randomLevel(rng, p, max) {
let lvl = 1;
while (lvl < max && rng() < p) lvl++; // geometric: P(level >= k) = p^(k-1)
return lvl;
}
function search(list, key) {
let x = list.head;
for (let l = list.level - 1; l >= 0; l--) // top lane first
while (x.next[l] && x.next[l].key < key) x = x.next[l];
x = x.next[0];
return x && x.key === key ? x : null;
} Watch the SEARCH COST box and the hops invariant. SORTED LINKED LIST runs the same inserts and searches and needs 32 hops against a bound of 12. SKIP LIST draws its levels from a seeded coin and its worst search over all 32 keys is 8. The bound is a probabilistic one: this seed meets it, and a different coin could miss it by a few hops. TRIE AUTOCOMPLETE shows the second invariant: every lookup visits at most as many nodes as the key has characters. That holds by construction, since a trie descends one node per character; what matters is that it never grows with the number of keys stored. Autocomplete then pays separately to list every key in the subtree under the prefix.
A plain sorted list costs n hops; the skip list bound grows as 2·log2 n + 2. Find the largest KEYS value at which the plain list still meets the bound.
GOAL: the plain list meets the bound with n >= 8
This randomLevel flips the coin once, so no node is taller than 2. Make it geometric: start at 1, add a level for every heads (rng() < p), stop at the first tails or at max. The grader builds three 1024-key skip lists from your levels and searches every key.
Large Redis sorted sets (ZSET) are a skip list plus a hash table. LevelDB and RocksDB memtables are skip lists, because concurrent inserts are easier than in a balanced tree. Tries (and their compressed form, radix trees) back IP routing tables, autocomplete, and the Linux page cache index (a radix tree, now the XArray).
“Design autocomplete” (trie, then top-k per node) or “why does Redis use a skip list for sorted sets?” Say: expected O(log n) search, simple insert with no rebalancing, easy range scans along level 1, and that the guarantee is probabilistic rather than worst case.