Design Skiplist - Problem

🎯 Design a Skip List Data Structure

Imagine having a multi-level highway system for data! A Skip List is a brilliant data structure that achieves O(log n) performance for search, insertion, and deletion operations using nothing more than clever layered linked lists.

Your task is to implement a Skip List from scratch that supports:

  • Search: Find if a target value exists
  • Add: Insert a new value (duplicates allowed)
  • Erase: Remove a value and return success status

The Magic: Unlike traditional linked lists that require O(n) traversal, Skip Lists use multiple levels where higher levels act as "express lanes" to jump over many elements quickly. Each element has a random height, creating a probabilistic balanced structure.

Example: For a Skip List containing [30, 40, 50, 60, 70, 90], adding 45 and 80 would create new nodes that randomly appear in multiple levels, maintaining the sorted order at each level.

Think of it as a linked list with turbo boosters!

Input & Output

example_1.py — Basic Operations
$ Input: skiplist = Skiplist() skiplist.add(1) skiplist.add(2) skiplist.add(3) skiplist.search(0) # return False skiplist.add(4) skiplist.search(1) # return True skiplist.erase(0) # return False skiplist.erase(1) # return True skiplist.search(1) # return False
Output: False True False True False
💡 Note: After adding 1,2,3: search(0) fails. Add 4: search(1) succeeds. erase(0) fails (not found). erase(1) succeeds. search(1) now fails.
example_2.py — Duplicate Handling
$ Input: skiplist = Skiplist() skiplist.add(5) skiplist.add(5) skiplist.add(5) skiplist.search(5) # return True skiplist.erase(5) # return True skiplist.search(5) # return True (still has duplicates) skiplist.erase(5) # return True skiplist.erase(5) # return True skiplist.erase(5) # return False
Output: True True True True True False
💡 Note: Skip List handles duplicates correctly. After adding three 5's, each erase removes one instance until all are gone.
example_3.py — Empty List Operations
$ Input: skiplist = Skiplist() skiplist.search(1) # return False skiplist.erase(1) # return False skiplist.add(1) skiplist.search(1) # return True
Output: False False True
💡 Note: Operations on empty Skip List work correctly. search and erase return False, but add followed by search works.

Constraints

  • 0 ≤ num, target ≤ 2 × 104
  • At most 5 × 104 calls will be made to search, add, and erase
  • Duplicates are allowed in the Skip List
  • Performance requirement: Average O(log n) time complexity for all operations

Visualization

Tap to expand
Design Skiplist - Data Structure INPUT Skip List Structure (4 levels) L3 L2 L1 L0 H H H H 1 1 2 2 2 3 4 4 4 4 Operations: add(1), add(2), add(3) search(0) --> False add(4) search(1) --> True erase(0) --> False erase(1) --> True search(1) --> False Time: O(log n) avg | Space: O(n) ALGORITHM STEPS 1 Search Operation Start from top-left head Move right while next < target Drop down if stuck, repeat 2 Add Operation Find insert position (search) Flip coin for random height Insert node, update pointers 3 Erase Operation Find node at each level Update prev pointers to skip Return False if not found 4 Level Management Max levels = 16 (typical) P = 0.5 probability Maintains balance probabilistically Search Path for value 3: H 2 2 3 Found! FINAL RESULT Operation Results: search(0) False search(1) True erase(0) False erase(1) True search(1) False Output Sequence: False, True, False, True, False Final Skiplist State: H 2 3 4 (Node 1 removed) Key Insight: Skip Lists achieve O(log n) operations through probabilistic balancing. Higher levels act as "express lanes" that skip over elements, reducing the search path. Unlike balanced BSTs, Skip Lists use randomization instead of rotations, making implementation simpler while maintaining efficiency. TutorialsPoint - Design Skiplist | Optimal Solution
Asked in
Amazon 15 Google 12 Redis Labs 8 Microsoft 6
28.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen