Hash Tables Practice Problems

Potentially useful: decimal to binary converter

Consider the following cuckoo hashing schema:1

  1. Insert keys 12 and 10. Draw the resulting two tables.
  2. Then delete 14, and insert 8. Draw the resulting two tables.
  3. Finally, insert 28. Draw the resulting two tables.

Consider an extendible hashing structure such that:

Starting from an empty table, insert keys 15, 2, 31, 11.2

  1. What is the global depth of the resulting table?
  2. What is the local depth the bucket containing 2?
  3. What is the local depth of the bucket containing 31?

Starting from the result of the above inserts, you insert keys 6, 9, 23, 12.3

  1. Which key will cause a split (without doubling the size of the table)?
  2. Which key will make the table double in size?

Footnotes:

1
  1. cuckoo-hashing-2.png
  2. cuckoo-hashing-3.png
  3. cuckoo-hashing-4.png
2

Initially there will be just one bucket, and 15 and 2 will both be stored there. When 31 is inserted, this bucket will split, and the global and local depths will increase from 0 to 1. 15 and 31 will hash to slot 1 and 2 to slot 0. 11 will also hash to bucket 1. Since that bucket's local depth is equal to the global depth, the table doubles in size, increasing the global depth to 2. Slots 00 and 10 will both refer to the bucket with 2, which remains unchanged. 15 and 31 rehash to slot 11. Slot 01 refers to a new, empty bucket. We re-attempt inserting 11, and it again collides with 15 and 31, triggering another table resize/global depth increase. With a depth of 3, both 15 and 31 rehash to slot 111, and fortunately 11 hashes to slot 011, allowing us to complete the insert.

extensible-hashing-1.png

  1. The global depth will be 3
  2. The bucket containing 2 remains unchanged since the first split, and so will have a depth of 1
  3. The bucket containing 31 will have a depth of 3
3
  1. 12 will hash to the bucket containing 6 and 2, but since the depth there is only 1, it can split without resizing the table
  2. 23 will hash to the bucket containing 15 and 31, causing the table to resize (double)