Hashing Problem Set Solutions
Hashing Problem Set Solutions
Instructors: Birrell, Gries, and Sampson Made by: Your 2110 TAs
1 Hash Functions
1. Suppose a.equals(b). What can you conclude about a.hashCode() and b.hashCode()?
(Assume hashCode() is properly implemented)
a.hashCode() == b.hashCode()
3. The best hash functions have the most amount of clustering. (T/F) False
4. Below is part of class HashMe. Circle the best hash function for it from the list
below. Also, underline any valid hash functions (they could be terrible, but as long
as they work). Assume that timeOfDayInSeconds() returns an int. Justify your
rationale
8-1
2 Time Complexity
You can look up answers to these questions in the Java API specs.
3 Collision Resolution
Assume that the following array backs a HashSet. The top line represents the index
and the bottom represents the value in the set. Each of the questions in this section are
independent of each other. Assume for this section that you don’t have to worry
about resizing.
0 1 2 3 4 5
MA NY VA
2. Assuming that we are using chaining and that CA hashes to index 3, which bucket
does CA end up in, and what is the size of the list in that bucket?
Bucket 3. Length of 2.
3. Assuming that we are using linear probing and that CA hashes to index 3, which
bucket does CA end up in, and how many state objects are in that bucket?
Bucket 4. 1 state object.
4. Assuming that we are using linear probing, CA hashes to index 3 and CA has
already been inserted. How many buckets would linear probing need to probe if we
were to insert AK, which also hashes to index 3? (The initial bucket check counts
as a probe)
5. Probes index 3, 4, 5, 0, then 1.
5. Assuming that we are using quadratic probing, CA hashes to index 3 and CA has
already been inserted. How many buckets would quadratic probing need to probe
if we were to insert AK, which also hashes to index 3?
3. Probes index 3, 4, 1. (3, 3 + 1, 3 + 22 )
8-2
4 Rehashing Practice
The following is the initial configuration of an array backing a HashSet. On the right is
a mapping from states to their hash values. Follow the directions below and draw the
backing array at the end of each step. Assume that the largest load factor allowed in the
HashSet is 12 and that it uses linear probing. If you have to increase the length of the
array, double its length.
State Hash
MA 6
NY 4
0 1 2 3 4 5
VA 5
MA VA
MO 14
AZ 2
PA 2
5 For Fun!
1. Hashing is used in many different asspects of computing. Do a Google search for
md5sum. Read up a little. Understand that that’s actually what you see in CMS
after you upload a document.
2. Now search for salted password hashing. Read about the role that hash functions
play in storing sensitive user data like passwords.
8-3