Skip to content

Hash table with open addressing

storing

Level 2 of 3

Also called Linear probing.

fresh numbers

The test's questions that a run answers, asked again on new numbers whenever you like. The answer, its working and the reason for every other choice come from hash table with open addressing's own run on the numbers shown.

choose one

question 1

An empty table of 7 slots puts in 12, 5, 19 and 6, in that order. How many probes do the four puts make in all?

the answer, and why

the answer is D

12, 5 and 19 have home 5; 6 has home 6. 12 takes slot 5, 1 probe. 5 walks to slot 6, 2 probes. 19 walks slots 5 and 6 to slot 0, 3 probes. 6 finds slot 6 taken by 5, then slot 0 by 19, and takes slot 1, 3 probes. That is 9.

  1. step 11 of 15

    012345619125

    6 leaves 6 when divided by 7, so its home is slot 6.

    slots looked at: 0

  2. step 12 of 15

    012345619125

    Slot 6 holds 5, not 6.

    slots looked at: 1

  3. step 13 of 15

    012345619125

    Slot 0 holds 19, not 6.

    slots looked at: 2

  4. step 14 of 15

    0123456196125

    Slot 1 is free, so put 6 there.

    slots looked at: 3

Operation 4 puts 6 in. Its home, slot 6, already holds 5, and slot 0 holds 19. 3 probes, though 6 shares its home with no number.

if you chose another

  • That counts 1 probe a put, as if every home were free.
  • That counts 6 as 1 probe, as if its home were free, but 5 took slot 6 on its walk.
  • That leaves out 6's put, which makes 3 probes.

new numbers

question 2

An empty table of 7 slots puts in 17, 24 and 38, then 31. Which slot does 31 go in?

the answer, and why

the answer is C

All four have home 3. 17 takes slot 3, 24 slot 4, 38 slot 5, so 31 walks 3, 4 and 5 to slot 6.

if you chose another

  • Slot 3 is 31's home, but 17 already holds it.
  • That stops at the first slot after its home, two too early; slot 4 holds 24.
  • That walks one slot too far; slot 6 is free.

new numbers