0

I have been going through the Reinforcement question in the textbook Data Structures and Algorithms in Java, 6th Edition, and came across this question:

"What would be a good hash code for a vehicle identification number that is a string of numbers and letters of the form “9X9XX99X9XX999999,” where a “9” represents a digit and an “X” represents a letter?"

My initial thought would to make the size of the hash table 100,003 and have it deal with the last 6 digits of the vehicle plate.

Am I right in considering this as a possible answer? Would love any feedback on this. Thanks!

kprog
  • 59
  • 2
  • 8
  • 8
    Without particular definition of "good" is it not possible to answer that question. Regular Java string's hash code would be fine for most cases. – Alexei Levenkov Jun 04 '17 at 21:26
  • All the standard Java API hash map implementations​, e.g., `HashMap`, have variable-size data structures. If you create over 100K bins but manage only 10 instances you have a lot of wasted bins. If you manage a billion instances you have a lot of collisions. It's better to use a standard hash and let the map grow as it accepts entries. If the idea is to customize the hash, the algorithm in Knuth's https://en.m.wikipedia.org/wiki/The_Art_of_Computer_Programming is suitable. You want to scramble the bits to get good dispersion of the values. – Lew Bloch Jun 04 '17 at 22:37
  • Do you have any preferred method like chaining or open addressing? For instance, hash function `h(k) = k mod m` can be a good choice for integer key `k` and hash table of size `m` which is a prime number. (It's trivial to transform string key to an integer). – karastojko Jun 05 '17 at 12:45

0 Answers0