BALT-P6

A skip list

Practice 6 of 6Balanced trees8 tests

The problem

Build a skip list (lesson 6) of different keys, with these methods:

  • add(key): put the key in. If it is already there, do nothing.
  • contains(key): True if the key is there.
  • remove(key): take the key out and return True; return False if it was not there.
  • first_at_least(key): the smallest key that is at least key, or None if there is none.
s = SkipList()
s.add(10); s.add(20); s.add(30)
s.first_at_least(15)    # 20
s.first_at_least(31)    # None
s.remove(20)            # True

Keep the shape from the starter code: self.head is a node whose list next has one link per level, and each key’s node has a list next as long as its tower. One test walks your levels through head.next[0], head.next[1] and head.next[2]: level 0 must hold every key in order. Give each new key a tower: start at level 0, then flip a fair coin (random.random() < 0.5 means heads). Each heads adds one level, and the first tails stops. So about half the keys reach level 1, and about a quarter reach level 2. The bisect module is switched off.

Limits

  • Up to 30,000 keys and about 50,000 operations; each should take O(log n) time on average.

Builds on BALT-6 Skip lists.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

8 tests, not run yet
Test results
#TestResultReads · time
1Add and findNot run
2Empty listNot run
3RemoveNot run
4First key at least xNot run
5RepeatsNot run
6Negative keysNot run
7The shapeNot run
8BigNot run