Skip to content

Hash table with double hashing

storing

Level 2 of 3

Also called Double hashing and Open addressing with a second hash.

test yourself

Six questions, four to pick from and two for paper. Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

An empty table of 7 slots puts in 3, 10, 17, 24 and 31, all with home 3. How many probes do the five puts make in all?

the answer, and why

the answer is A

3 takes slot 3, 1 probe. Each of the others has its own step, 1, 3, 5 and 2, and finds slot 3 taken, then a free slot. 1 + 2 + 2 + 2 + 2 is 9.

  1. step 17 of 21

    01234562431017

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

    slots looked at: 0

  2. step 18 of 21

    01234562431017

    31 leaves 1 when divided by 5, so its step is 2: each move goes 2 slots on, round to the start.

    slots looked at: 0

  3. step 19 of 21

    01234562431017

    Slot 3 holds 3, not 31.

    slots looked at: 1

  4. step 20 of 21

    0123456243103117

    Slot 5 is free, so put 31 there.

    slots looked at: 2

Operation 5 puts 31 in, step 2. Slot 3 holds 3, and slot 5 is free. 2 probes, though four numbers came first.

if you chose another

  • That walks one slot on each time, as linear probing does, 1 + 2 + 3 + 4 + 5.
  • That counts 1 probe a put, but each after 3 finds slot 3 taken.
  • That counts 2 probes for 3 too, but its home is free.

question 2

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

the answer, and why

the answer is A

17 takes slot 3. 10, home 3 and step 1, takes slot 4. 24 has home 3 and step 5. Slot 3 holds 17, and 3 + 5 is 8, which leaves 1 after dividing by 7. Slot 1 is free.

if you chose another

  • Slot 4 already holds 10. 24's step is 5, so from slot 3 it moves to slot 1.
  • Slot 3 is 24's home, but 17 already holds it.
  • That walks one slot on each time, 3, 4, 5, as linear probing does. 24's step is 5.

question 3

Say a table had 6 slots, and a number with home 0 had a step of 2. Which slots could its walk ever look at?

the answer, and why

the answer is D

0, then 2, then 4, then 6, which leaves 0 after dividing by 6, so back to 0. 2 shares the factor 2 with 6, so slots 1, 3 and 5 are never looked at.

this tests best, worst and average, on the learn page

if you chose another

  • Only a step sharing no factor with 6, such as 1 or 5, meets all 6.
  • After 2 comes 4, which is still inside the 6 slots.
  • Slots 1 and 5 are missed too. Every slot the walk meets is even.

question 4

Which is true of this page's table of 7 slots, with different numbers put in?

the answer, and why

the answer is A

The 7 slots are full after 7 numbers, so every later walk meets every slot and finds none free.

this tests where the formula comes from, on the learn page

if you chose another

  • They go different ways at the first move unless their steps agree. 10 and 24 share home 3, with steps 1 and 5.
  • A step of 7 lands back on the home every time, as 7 leaves 0 after dividing by 7.
  • From the 8th on, both look at all 7 slots.

pen and paper

question 5about 6 minutes

In a table of 7 slots, every slot holds a number except slot 5. A put of a new number has step 3, and its home is any of the 7 slots, each as likely.

  1. For each home, 0 to 6, how many probes does the put make?
  2. What is the average?
  3. Check it against n+12\frac{n + 1}{2}, the formula of section 4, with nn the 7 slots.
a hint

From each home, add 3 each time, keeping the remainder after dividing by 7, until slot 5.

a worked answer

Home 5 makes 1 probe, home 2 makes 2, home 6 makes 3, home 3 makes 4, home 0 makes 5, home 4 makes 6 and home 1 makes 7. They add to 28, and 28 over 7 is 4. The formula with 7 slots gives 8 over 2, also 4.

homeits walkprobes
551
22, 52
66, 2, 53
33, 6, 2, 54
00, 3, 6, 2, 55
44, 0, 3, 6, 2, 56
1all 77
Each home a different distance from slot 5 along a step of 3, 1 to 7.
Here let nn count slots, not numbers put in. A table of nn slots has one free slot. A put of a new number has a step that is the same whatever its home. Its walk meets every slot once before any twice, as each step 1 to 5 does in 7 slots. How many probes does it make, on average over its home?
  1. the move: walk to the free slot

    the line:
    the put probes from its hometo the free slot: 1 up to n\begin{gathered} \text{the put probes from its home} \\ \text{to the free slot: 1 up to } n \end{gathered}
    homeits walkprobes
    551
    22, 52
    66, 2, 53
    33, 6, 2, 54
    00, 3, 6, 2, 55
    44, 0, 3, 6, 2, 56
    11, 4, 0, 3, 6, 2, 57
    7 slots, only slot 5 free, step 3. Every walk is the end of 1, 4, 0, 3, 6, 2, 5, so each home gives a different number of probes, 1 to 7.

    why it is allowed: Its walk meets every slot once before any twice, as the question says. Walk back from the free slot by the step: that meets each home once, so each home is a different number of probes away, 1 up to nn. Its home is any of the nn slots, each as likely. This prices a put when one slot is left.

  2. the move: average over the homes

    the line:
    L=1+2+⋯+nnL = \frac{1 + 2 + \dots + n}{n}

    why it is allowed: LL is the probes averaged over the nn homes. By line 1, the homes give 1 up to nn, one each. For 7, 28 over 7.

  3. the move: use the page on adding up

    the line:
    L=n(n+1)2nL = \frac{\frac{n(n + 1)}{2}}{n}
    where does this come from?
    question 1 of the page adding up 1+2+⋯+n1 + 2 + \dots + nFit two staircases of squares together. What shape do they make?
    1. the move: draw the sum as a staircase

      the line:
      S=1+2+⋯+nS = 1 + 2 + \dots + n

      why it is allowed: Let SS be the sum of the whole numbers from 1 to nn. Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to nn. For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.

    2. the move: take two staircases

      the line:
      2S=2(1+2+⋯+n)2S = 2(1 + 2 + \dots + n)

      why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.

    3. the move: fit them into a rectangle

      the line:
      2S=n(n+1)2S = n(n + 1)
      Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15.

      why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds nn standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds n+1n + 1, and the nn columns fill a rectangle nn wide and n+1n + 1 tall.

    4. the move: halve both sides

      the line:
      S=n(n+1)2S = \frac{n(n + 1)}{2}

      why it is allowed: Halving both sides of line 3 keeps it true and leaves one SS. For 5, half of 30 is 15.

    the whole page: adding up 1+2+⋯+n1 + 2 + \dots + n

    why it is allowed: The box under this line shows that 1 up to a last number adds to that number times one more, halved. Here the last number is nn. For 7, 7 times 8, halved, is 28.

  4. the move: put the two divisions under one line

    the line:
    L=n(n+1)2nL = \frac{n(n + 1)}{2n}

    why it is allowed: Halved, then over nn, is over twice nn. For 7, 7 times 8 over 14 is 4.

  5. the move: tidy

    the line:
    L=n+12L = \frac{n + 1}{2}

    why it is allowed: The nn on top and below cancel. For 7 slots, 8 over 2 is 4. Section 3's 7th put averages about 3.99 instead, as there the step changes with the home.

try it with a small nn: at n=7n = 7, line 2 gives L=4L = 4, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • 1 to 7 probes, one for each home.
  • The average, 28 over 7, 4.
  • The formula giving 4 at 7 slots.

question 6about 8 minutes

An empty table of 7 slots puts in 17, 10 and 24, takes 24 out, then puts in 59.

  1. Where does each of 17, 10 and 24 go?
  2. What does taking 24 out leave?
  3. Which slot does 59 go in, and after how many probes?
a hint

17, 10, 24 and 59 all have home 3. A put notes the first marker it passes, but walks on to a free slot first.

a worked answer

17 takes slot 3, 10 slot 4 (step 1) and 24 slot 1 (step 5). Taking 24 out leaves a marker in slot 1. 59 has step 5 too. It looks at slot 3 (17), slot 1 (the marker, noted) and slot 6, free: 3 probes. 59 is nowhere further along, so it goes in slot 1, where the marker was.

  1. step 18 of 23

    0123456×1710

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

    slots looked at: 0

  2. step 19 of 23

    0123456×1710

    59 leaves 4 when divided by 5, so its step is 5: each move goes 5 slots on, round to the start.

    slots looked at: 0

  3. step 20 of 23

    0123456×1710

    Slot 3 holds 17, not 59.

    slots looked at: 1

  4. step 21 of 23

    0123456×1710

    Slot 1 holds a marker left by a number taken out. Note it: 59 can go here if it is nowhere further along.

    slots looked at: 2

  5. step 22 of 23

    0123456591710

    59 is nowhere further along, so put it in slot 1, where the first marker was.

    slots looked at: 3

Operation 5 puts 59 in, step 5. It notes the marker in slot 1, walks on to the free slot 6, then goes in slot 1. 3 probes.

this tests how it works, on the learn page

what a full answer includes
  • Slots 3, 4 and 1.
  • A marker in slot 1.
  • Slot 1, after 3 probes.