Design a Skiplist
🪜 What is a skip list? It is a sorted linked list with express lanes stacked on top. The bottom lane holds every value. Each lane above skips over more and more values, so a search can race along the top and only drop down when it is about to overshoot. Search, insert and delete all take
O(log n)time on average, with no tree rotations.
A skip list sits behind Redis sorted sets, the in-memory tables of LevelDB and RocksDB, and Java's ConcurrentSkipListMap. In an interview it is a data-structure design question: there is no business domain, so the interviewer watches how you pick the structure, keep its invariants straight while you rewire pointers, and make something random still testable. This post builds it step by step, from a plain sorted list to the full design.
Requirements
"Design a skip list. It should support search, add and erase in
O(log n)on average. Don't use a built-in sorted library."
That is the whole prompt. A few details change the code, so pin them down before drawing anything.
Clarifying Questions
You: Can the same value be added more than once?
Interviewer: Yes. Keep every copy. erase removes one copy at a time.
You: What do the methods return? Especially erase when the value is not there?
Interviewer: search returns true or false. add returns nothing. erase returns true if it removed a copy and false if the value was not there.
You: Is O(log n) on average good enough, or do you need a worst-case bound?
Interviewer: Average is fine. If you need worst case, you would build a balanced tree, and that is a different question.
You: Can I use randomness? And if I do, how do we test it?
Interviewer: Randomness is expected. Tell me how you would make a run reproducible.
You: Is it just numbers, or keys with values like a map?
Interviewer: Just integers for now, between 0 and 20,000. We may turn it into a map later.
You: Will several threads use it at once?
Interviewer: Single thread for now. We will come back to that.
The fourth question is the one most candidates skip. A design that calls random() deep inside a method is hard to debug, because the bug you saw once may not happen again. Asking up front tells the interviewer you have tested code like this before.
💡 Interview tip: For a pure data-structure question, the "requirements" are the operations, their return values, the duplicate rule and the cost bound. Write those four things on the board before you write a class. They are your contract, and every later choice is checked against them.
Final Requirements
search(target)returns whethertargetis stored.add(num)storesnum. Duplicates are allowed and every copy is kept.erase(num)removes one copy ofnumand returnstrue, or returnsfalseand changes nothing ifnumis not stored.- All three run in
O(log n)expected time, wherenis the number of stored values. No built-in sorted container. - Randomness is allowed, but a run must be reproducible: given the same seed, the structure ends up the same.
Out of scope
- Keys with values, range queries and "what is the rank of x" (Extensibility).
- Several threads at once (Extensibility).
- Persistence to disk.