Design a skip list with probabilistic balancing. The insert operation flips a coin to determine new node tower height. Search, insert, and delete must achieve O(log n) average time complexity.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
The skip list consists of multiple sorted levels of nodes, each with forward pointers that "skip" ahead. Each node's height is chosen probabilistically by repeatedly flipping a coin (with probability 0.5 to increase level) up to a maximum level, producing a geometric distribution of heights. A sentinel head node holds pointers at the current max level.
Search starts from the top level, moving forward while next nodes have keys less than the target, then drops down levels until reaching level 0. Insert first finds update points at each level, then generates a random height, creates the node, and updates forward pointers accordingly. Delete locates the node and adjusts pointers to remove it.
This structure ensures that each level has about half the nodes of the level below, leading to expected O(log n) search, insert, and delete time, with O(n) space. The coin-flip balancing probabilistically maintains a logarithmic number of levels.
A skip list is a hierarchy of ordered linked lists that lets you “zoom” through the data at multiple resolutions. Each element appears in the bottom list (level 0), and with some probability it also appears in higher lists. The tower of forward pointers associated with a key looks like:
A simple implementation keeps a head sentinel with the maximum number of levels and a current highest level. Nodes carry an array next[0..height-1] of forward pointers.
A common choice for the height distribution is geometric with parameter ½: the probability that a node has height at least i is (2^{-i}). You can generate it by “flipping a coin”:
pseudo
randomLevel():
level ← 1
while level < MAX_LEVEL and coinFlipIsHeads(): // e.g. rand() < 0.5
level ← level + 1
return level
The fundamental operations use the same top‑down search:
pseudo
search(key):
x ← head
for i from currentLevel-1 downto 0:
while x.next[i] ≠ null and x.next[i].key < key:
x ← x.next[i]
x ← x.next[0]
if x ≠ null and x.key = key:
return x
else:
return null
Insertion uses the search to remember the last node visited at each level (the “update” array), then splices in a new tower of the chosen height:
pseudo
insert(key, value):
update[0..MAX_LEVEL-1] // array of predecessors at each level
x ← head
for i from currentLevel-1 downto 0:
while x.next[i] ≠ null and x.next[i].key < key:
x ← x.next[i]
update[i] ← x
x ← x.next[0]
if x ≠ null and x.key = key:
x.value ← value // overwrite existing
return
lvl ← randomLevel()
if lvl > currentLevel:
for i from currentLevel to lvl-1:
update[i] ← head
currentLevel ← lvl
newNode ← Node(key, value, lvl)
for i from 0 to lvl-1:
newNode.next[i] ← update[i].next[i]
update[i].next[i] ← newNode
Deletion is similar, rewiring pointers that point to the node to be deleted:
pseudo
delete(key):
update[0..MAX_LEVEL-1]
x ← head
for i from currentLevel-1 downto 0:
while x.next[i] ≠ null and x.next[i].key < key:
x ← x.next[i]
update[i] ← x
x ← x.next[0]
if x = null or x.key ≠ key:
return false // not found
for i from 0 to currentLevel-1:
if update[i].next[i] ≠ x: break
update[i].next[i] ← x.next[i]
while currentLevel > 1 and head.next[currentLevel-1] = null:
currentLevel ← currentLevel - 1
return true
With the ½ coin‑flip height rule, the expected number of nodes at level i is (n/2^i), so the expected number of levels is (O(\log n)) and the expected length of a search path is also (O(\log n)). Inserting and deleting perform a search plus a constant amount of work per level of the new node, so they run in expected (O(\log n)) time as well. The random heights serve as a probabilistic balancing mechanism; no explicit rebalancing is needed.