DSA Notes
Learn skip lists as probabilistic alternative to balanced trees.
What is a Skip List?
A Skip List is a probabilistic data structure that provides O(log n) average-case performance for search, insertion, and deletion operations. It is an alternative to balanced binary search trees (AVL trees, Red-Black trees) that achieves balance through randomization rather than complex rotation rules.
Think of a skip list like an express train system. The bottom level has every station (every element in sorted order). Higher levels skip some stations, acting like express lines. To find a station, you start on the highest express line and drop down levels as needed. This gives you logarithmic search time without the complexity of tree rebalancing.
Implementation in Python
Complexity Analysis
| Operation | Average Case | Worst Case | Space |
|---|---|---|---|
| Search | O(log n) | O(n) | - |
| Insert | O(log n) | O(n) | O(log n) per node |
| Delete | O(log n) | O(n) | - |
| Space (total) | O(n) | O(n log n) | - |
The expected number of levels is O(log n) with probability p = 1/2. Each node has an expected 2 forward pointers (1 + 1/2 + 1/4 + ... = 2), so total expected space is O(n).
Skip List vs Balanced BSTs
| Feature | Skip List | AVL/Red-Black Tree |
|---|---|---|
| Implementation complexity | Simple | Complex (rotations) |
| Concurrency | Easy to lock-free | Difficult |
| Cache performance | Good (sequential) | Poor (pointer chasing) |
| Worst case guarantee | No (probabilistic) | Yes (deterministic) |
| Range queries | Natural (forward pointers) | Requires in-order traversal |
| Space overhead | ~2 pointers per node (avg) | 2-3 pointers per node |
| Insertion/Deletion | No rebalancing needed | Rotations required |
Why Skip Lists Matter in Practice
Skip lists are used in production systems because of their simplicity and excellent concurrent behavior:
- Redis — The sorted set data structure in Redis is implemented using skip lists. Redis chose skip lists over balanced trees because they are simpler to implement, easier to debug, and support range queries naturally.
- LevelDB/RocksDB — Google's LevelDB uses skip lists for its in-memory component (memtable). The simplicity and good cache behavior make them ideal for this use case.
- Apache Lucene — The search engine library uses skip lists in its posting list structure for fast boolean query processing.
- Concurrent programming — Lock-free skip lists are simpler to implement than lock-free balanced trees, making them popular in concurrent data structures.
The Probability Parameter (p)
The promotion probability p affects the trade-off between space and time:
| p value | Expected levels | Space per node | Search time |
|---|---|---|---|
| 1/2 | log₂(n) | 2 pointers | O(log n) |
| 1/4 | log₄(n) | 4/3 pointers | O(log n) |
| 1/e | logₑ(n) | e/(e-1) pointers | O(log n) |
p = 1/2 is most common. p = 1/4 uses less space at the cost of slightly more comparisons per search.
Interview Questions
Q1: Why choose a skip list over a balanced BST? A: Skip lists are simpler to implement (no rotations), easier to make concurrent (lock-free variants), and support range queries naturally via forward pointers. Choose them when implementation simplicity or concurrency matters more than worst-case guarantees.
Q2: What is the expected height of a skip list with n elements? A: O(log n) with high probability. Specifically, with p = 1/2, the expected max level is log₂(n).
Q3: Can a skip list degrade to O(n) performance? A: Yes, if all coin flips result in level 0 (extremely unlikely). The probability of this is (1-p)^n, which is astronomically small for any reasonable n.
Q4: How does Redis use skip lists? A: Redis sorted sets (ZSET) use a skip list combined with a hash map. The skip list maintains sorted order for range queries and ranking, while the hash map provides O(1) lookups by member name.
Q5: How would you implement a lock-free skip list? A: Use CAS (Compare-And-Swap) operations for pointer updates. Mark nodes for deletion before physically removing them. The layered structure means conflicts are localized to specific levels, reducing contention compared to tree rotations.
Key Takeaways
- Skip lists achieve O(log n) operations through randomization rather than complex rebalancing
- They are simpler to implement than AVL or Red-Black trees
- They excel in concurrent programming due to easy lock-free implementations
- Redis and LevelDB use skip lists in production for their sorted data structures
- The trade-off is probabilistic rather than deterministic guarantees
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Skip List - Probabilistic Balanced Structure.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, advanced, topics, skip
Related Data Structures & Algorithms Topics