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.

Visualization

Tap to expand
Skip List: Multi-Level Highway SystemLevel 3: Super Express90โˆžLevel 2: Express Lane507090Level 1: City Streets4050708090Level 0: Every Building30405060708090STSearch path (red dashed): Start express โ†’ Drop when needed โ†’ Find target efficiently!
Understanding the Visualization
1
Express Lane Start
Begin at the highest available level to skip maximum elements
2
Horizontal Travel
Move right on current level while next value is less than target
3
Drop Down
When can't go right, drop to lower level for more precision
4
Target Reached
Continue until target found at level 0 or confirmed absent
Key Takeaway
๐ŸŽฏ Key Insight: Skip Lists use randomization to create a naturally balanced multi-level structure, achieving tree-like performance with linked list simplicity!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(log n)

Expected O(log n) for all operations due to probabilistic balancing through random heights

n
2n
โšก Linearithmic
Space Complexity
O(n)

Linear space for nodes, with expected O(n) total pointers across all levels

n
2n
โšก Linearithmic Space

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
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