h(key) = key % 5. Keys are inserted in this order: 12, 7, 22, 17, 5. What is the state of the hash table after all insertions? Each bucket stores keys in insertion order using chaining (linked list).Buckets: 0: 1: 2: 3: 4: Insert keys: 12, 7, 22, 17, 5 Hash function: h(key) = key % 5 Show each bucket's keys separated by ' -> ' and ending with 'null'.
Calculate key % 5 for each key:
- 12 % 5 = 2 → bucket 2
- 7 % 5 = 2 → bucket 2
- 22 % 5 = 2 → bucket 2
- 17 % 5 = 2 → bucket 2
- 5 % 5 = 0 → bucket 0
Keys are inserted in order into their respective buckets using chaining. Bucket 0: 5 -> null. Bucket 2: 12 -> 7 -> 22 -> 17 -> null. Other buckets empty. This matches option C.
The hash function must always return the same hash value for the same input to ensure that the key can be found in the hash table. Producing unique hash values for every input is impossible due to limited bucket size. Speed is important but not the most critical property here. Producing always even numbers would cause poor distribution.
key % size but has a bug. What error will it cause?int hashFunction(int key, int size) { return key / size; } // size is the number of buckets
The function uses division / instead of modulo %. This will return the quotient, not the remainder, so keys will be mapped incorrectly to buckets.
Summing ASCII values considers all characters, helping spread keys better. Using length or first character alone causes many collisions. Using a constant value puts all keys in one bucket, causing worst performance.
int h(int key) { return (key * 7 + 3) % 3; }Keys inserted in order: 1, 4, 7, 10.
Collision is handled by chaining (linked list). What is the final state of the hash table?
Buckets: 0: 1: 2: Insert keys: 1, 4, 7, 10 Hash function: h(key) = (key * 7 + 3) % 3 Show each bucket's keys separated by ' -> ' and ending with 'null'.
Calculate each key's bucket:
- h(1) = (1*7+3)%3 = 10%3 = 1
- h(4) = (4*7+3)%3 = 31%3 = 1
- h(7) = (7*7+3)%3 = 52%3 = 1
- h(10) = (10*7+3)%3 = 73%3 = 1
All keys map to bucket 1: 1 -> 4 -> 7 -> 10 -> null. Buckets 0 and 2 empty. This is option D.
