HASH-P2

Probing with markers

Practice 2 of 6Hashing11 tests

The problem

Build the table from the figure in lesson 3: m slots, keys are whole numbers, and a key’s home slot is key % m. If a slot is taken, try the next one, (i + 1) % m. The table never grows.

  • contains starts at the home slot and probes until it finds the key (True) or an empty slot (False). It skips 'DEL' markers. It never looks at more than m slots.
  • remove finds the key the same way and puts 'DEL' in its slot.
  • insert does nothing if the key is already there, and returns its slot. Otherwise it puts the key in the first 'DEL' slot it passed, or else in the empty slot where it stopped, and returns that slot. If it finds neither in m probes, it returns -1.
T = ProbeTable(10)
T.insert(23); T.insert(43); T.insert(13)   # 3, 4, 5
T.remove(43)                               # True
T.slots()
# [None, None, None, 23, 'DEL', 13,
#  None, None, None, None]
T.contains(13)                             # True

The tests make an object and call its methods in order. A test shows the list of calls, and compares the list of everything they returned (methods that return nothing give None).

Limits

  • Up to 131,072 slots and 100,000 keys. Keys may be negative.

Builds on HASH-3 Open addressing and load factor.

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.

11 tests, not run yet
Test results
#TestResultReads · time
1The figure's insertsNot run
2FindNot run
3Insert a key twiceNot run
4Wrap around the endNot run
5Remove leaves a markerNot run
6Insert reuses a markerNot run
7No copy after a markerNot run
8Remove a missing keyNot run
9A full tableNot run
10Negative keysNot run
11100,000 keysNot run