Hash Table Exercises (in class)

1) Given the values {2341, 4234, 2839, 430, 22, 397, 3920}, a hash table of size 7, and hash function h(x) = x mod 7, show the resulting tables after inserting the values in the given order with each of these collision strategies.

  1. Using separate chaining
  2. Using linear probing
  3. Using quadratic probing (note if it fails for any values)
  4. Using double hashing with second hash function h'(x) = (2x - 1) mod 7 (note if it fails for any values)
strategy[0][1][2][3][4][5][6]
chaining       
linear probe       
quadratic probe       
double hashing       

2) Suppose you need to insert unique 3-character IDs into a hash table, where each ID is made up of some combination of two of the capital letters A-D, followed by one of the lower case letters x-z, such as: ABx, DCy, BBz, etc. Repeat letters are allowed in an ID.

  1. How many possible codes are there?
  2. Assume that chaining is used to resolve collisions. How big would you make the table and what hash function would you use so that at most 2 codes are in each bucket at any point in time? Write your hash function as a Java method which takes the ID as a parameter and returns the hash code index to use.

3) Suppose a hash table with capacity M=31 gets to be over 3/4ths full. We decide to rehash. What is a good size choice for the new table to reduce the load factor below .5 and also avoid collisions?

4) Suppose you are running a food service business which has special promotions for frequent customers. Each month you send out discount offers to different groups based on their spending in your establishment. For example, one month those who spent $25-50 might get a free drink coupon, or another month those who spent $50-100 might get a 2-for-1 lunch offer. The spending ranges will change each month, and you want to be able to find the right group of people each month in O(K + log N) time, where K is the size of the group and N is the total number of customers. What data structure(s) would you choose and how would you implement a solution to this problem? (sketch briefly)

5) Suppose you decide to use a map to store transcript information. In addition to adding items each semester, you will use this transcript to find out information about discipline specific groups of courses - such as the number of math credits earned or the GPA for all your computer science classes. What data member would you use as the key for each course, and how exactly would you implement it to optimize performance? Your options are: a linked list (ordered, unordered, singly linked, double linked), a binary search tree (plain, AVL, splay), a hash table (chaining, linear probing or quadratic probing), or some combination. Be detailed and explain all your choices.