Consider a dynamic hashing approach for 64-bit integer keys: 1. There is a main hash table of size 4. 2. The least significant 2 bits of a key is used to index into the main hash table. 3. Initially, the main hash table entries are empty. 4. Thereafter, when more keys are hashed into it, to resolve collisions, the set of all keys corresponding to a main hash table entry is organized as a binary tree that grows on demand. 5. The 3rd least significant bit is used to divide the keys into left and right subtrees. 6. To resolve more collisions, each node of the binary tree is further sub-divided into left and right subtrees based on the 4th least significant bit. 7. A split is done only if it is needed, i.e., only when there is a collision. Consider the following state of the hash table. [The hash table has: slot 0b00 -> a binary tree with 5,9,13,7 arranged; slot 0b10 -> contains 10; slot 0b01 and 0b11 are empty] Which of the following sequences of key insertions can cause the above state of the hash table (assume the keys are in decimal notation)? A. 5, 9, 4, 13, 10, 7 B. 9, 5, 13, 10, 7, 5, 13 C. 10, 9, 7, 5, 13 D. 5, 9, 13, 6, 14, 10, 14

GATE 2021 · Algorithms · Hashing · medium

Answer: C. 10, 9, 7, 5, 13

  1. Determine slot assignments from binary representations: Key 5 = 0101: 5 mod 4 = 1 -> slot 01. Key 9 = 1001: 9 mod 4 = 1 -> slot 01. Key 13 = 1101: 13 mod 4 = 1 -> slot 01. Key 7 = 0111: 7 mod 4 = 3 -> slot 11. Key 10 = 1010: 10 mod 4 = 2 -> slot 10. Key 4 = 0100: 4 mod 4 = 0 -> slot 00. Key 6 = 0110: 6 mod 4 = 2 -> slot 10. Key 14 = 1110: 14 mod 4 = 2 -> slot 10.
  2. Verify tree structure for slot 01 using bit 2 (3rd LSB): For keys in slot 01: Key 5=0101: bit2 = (5>>2)&1 = 1&1 = 1 -> right subtree. Key 9=1001: bit2 = (9>>2)&1 = 2&1 = 0 -> left subtree. Key 13=1101: bit2 = (13>>2)&1 = 3&1 = 1 -> right subtree. So 9 goes left, 5 and 13 both go right (collision at right child). For 5 and 13 at right child: use bit3. Key 5=0101: bit3=(5>>3)&1=0 -> left. Key 13=1101: bit3=(13>>3)&1=1 -> right. Tree for slot01: root splits on bit2: left=9, right splits on bit3: left=5, right=13. For option C (10,9,7,5,13): no key 4, no key 6 or 14. Slot01 has 9,5,13. Slot10 has 10. Slot11 has 7. This matches the image description.
  3. Eliminate other options: Option A has key 4 (slot00) and key 7 (slot11) - 4 would appear in slot00 making it non-empty, and image likely shows slot00 empty. Option B has repeated keys 5 and 13 (appears twice each) - a set cannot have duplicates. Option D has repeated key 14 - invalid. Option C: all unique keys, and slots match the described state.