ProgrammingVery hard
Which expression always gives a valid bucket index in the range 0 to m - 1 for a signed int key k, with m > 0?
- A. k % m
- B. ((k % m) + m) % m
- C. (k % m) + m
- D. k / (m + 1)
Correct answer: B. ((k % m) + m) % m
C99 % keeps the sign of the dividend, so k % m can be negative for negative k. Adding m and taking % again gives a value in range.
More C Programming questions
- A chained hash table is resized by doubling whenever it becomes full, starting at capacity 1. Inserting 8 keys, how many element moves…
- Keys 8, 16, 24 and 32 are hashed with h(k) = k % 8 and then with h(k) = k % 7. Which statement is correct?
- With 10^5 keys and a load factor of at most 0.5, what is the smallest power-of-two table size that works?
- Keys 13 and 23 are inserted with linear probing into a table of size 10 with h(k) = k % 10. In which slot does 23 land?
- Keys 6, 1 and 11 are inserted with linear probing into a table of size 5, h(k) = k % 5. In which order do the keys appear when the table…
- With chaining, what is the expected number of nodes examined by an unsuccessful search when the load factor is 1.5?