Skip to content

Hash table with chaining

storing

Level 2 of 3

Also called Separate chaining.

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 chaining's own run on the numbers shown.

choose one

question 1

An empty table of 5 buckets puts in 23, 41, 18, 33 and 6, in that order. How many looks do the five puts make in all?

the answer, and why

the answer is C

23 and 41 find empty buckets, 0 looks. 18 meets 23 in bucket 3, 1. 33 meets 23 and 18, 2. 6 meets 41 in bucket 1, 1. That is 4.

  1. step 9 of 16

    3341231801234

    33 leaves 3 when divided by 5, so it belongs in bucket 3.

    compared: 0

  2. step 10 of 16

    3341231801234

    23 is not 33.

    compared: 1

  3. step 11 of 16

    3341231801234

    18 is not 33.

    compared: 2

  4. step 12 of 16

    4123183301234

    33 is not in bucket 3 yet, so put it in.

    compared: 2

Operation 4 puts 33 in. Bucket 3 holds 23 and 18, so 2 looks. Yellow is being compared, red just put in.

if you chose another

  • That counts every number before each put, as if all shared one bucket.
  • That counts only 33's 2 looks, but 18 meets 23 and 6 meets 41 too.
  • That counts 1 look a put after the first two, but 33 meets two numbers.

new numbers

question 2

Which five numbers, put into an empty table in this order, cost the fewest looks?

the answer, and why

the answer is D

11, 22, 33, 44 and 55 leave 1, 2, 3, 4 and 0, a bucket each, so no put meets a number. 0 looks.

55011111222133314441slotkeys
11, 22, 33, 44 and 55, one to a bucket.

if you chose another

  • All five leave 0 and share bucket 0, the dearest spread, 10 looks.
  • 12, 17 and 22 share bucket 2, and 3 and 8 share bucket 3, 4 looks.
  • 15, 25 and 35 share bucket 0, and 16 and 26 share bucket 1, 4 looks.

new numbers

question 3

An empty table puts in 12, 7 and 32, takes 7 out, then puts in 22. How many looks do the five operations make?

the answer, and why

the answer is C

12 finds bucket 2 empty, 0. 7 meets 12, 1. 32 meets 12 and 7, 2. Taking 7 out meets 12, then finds 7, 2. 22 meets 12 and 32, 2. That is 7.

  1. step 11 of 19

    71273201234

    7 leaves 2 when divided by 5, so if it is here at all, it is in bucket 2.

    compared: 0

  2. step 12 of 19

    71273201234

    12 is not 7.

    compared: 1

  3. step 13 of 19

    71273201234

    Here is 7, in bucket 2.

    compared: 2

  4. step 14 of 19

    123201234

    Take 7 out of bucket 2.

    compared: 2

Operation 4 takes 7 out. It meets 12, then finds 7, 2 looks, and 7 leaves bucket 2. Yellow is being compared, and 7 is filled in when found.

if you chose another

  • That counts the take as a walk of the whole bucket, but it stops at 7.
  • That leaves out the take's 2 looks.
  • That counts the take as 1 look, but it meets 12 before it finds 7.

new numbers