Practice this topic in a realistic system design interview
Many systems need to keep data sorted while it changes all the time. Leaderboards, in-memory database buffers, and sorted maps all need fast lookups, fast updates, and fast range scans at the same time.
A skip list is a sorted linked list with extra shortcut lanes above it. Those lanes let a search jump over large parts of the list instead of walking one node at a time.
In this chapter, we'll learn what a skip list is, how it uses randomness to keep a sorted collection fast to search and update, and why systems like Redis, RocksDB, and Java's concurrent collections use it.
Suppose you're building a leaderboard for an online game with millions of players. Each player has a score, and these scores change constantly as people play.
The leaderboard needs to support several operations efficiently:
To support these operations, the data needs to stay sorted by score. And with thousands of score updates happening every second, we need a data structure that can handle frequent insertions and deletions without becoming a bottleneck.
The simplest option is to use a sorted array. Searching is fast because we can use binary search.
But inserting a new score is expensive because we may need to shift all the elements that come after it to make room. With millions of players, a single insertion could mean moving millions of elements.
Another option is a sorted linked list. Here, insertion is cheap once we know where the new element belongs. We just need to update a few pointers.
The problem is finding that position. Unlike an array, a linked list cannot jump directly to the middle. We have to start at the beginning and move through the nodes one by one:
| Structure | Search | Insert / Delete |
|---|---|---|
| Sorted array | Fast: O(log n) with binary search | Slow: O(n), elements must shift |
| Sorted linked list | Slow: O(n), walk node by node | Fast once the position is found |
So a sorted array gives us fast searches but slow updates, while a sorted linked list makes updates easy but searches slow. What we really want is a data structure that can do both efficiently.
Balanced binary search trees, like red-black trees and AVL trees, can solve this problem. They keep data sorted and support searching, inserting, and deleting in logarithmic time.
However, they also come with some challenges:
Skip lists offer similar performance with a simpler structure that's easier to implement and can be more concurrency-friendly.