Example: Hash Functions for Scanner Codes

A hash function deterministically maps each key to an integer in a specified range. This example follows a warehouse scanner that converts two-letter item codes into indices from 0 through 10.


Example: The scanner accepts uppercase letters and assigns A = 1, B = 2, continuing through Z = 26. For a key ss, let xx be the first letter's value and yy the second letter's value. It first calculates an integer hash code C(s)C(s), then reduces that code:


C(s)=31x+y,h(s)=C(s) mod 11.C(s)=31x+y, \qquad h(s)=C(s)\bmod11.


Modulo returns the nonnegative remainder after division by 11. All calculations use exact integer arithmetic, with no overflow. Use D = 4, L = 12, and W = 23 for the following parts.


Three connected boxes show a key becoming an integer code and then an index.


a) Find the index for DL.


For DL, the first letter contributes 31⋅431\cdot4, which is ________. Add the second letter's value:


C(DL)=31⋅4+12=124+12=136.C(\text{DL})=31\cdot4+12=124+12=136.


The largest multiple of 11 no greater than 136 is ________, obtained from 11⋅1211\cdot12. Subtract it to find the remainder:


h(DL)=136 mod 11=136−132=4.h(\text{DL})=136\bmod11=136-132=4.


The index for DL is 4. Indices are dimensionless, and 4 lies within the required range from 0 through 10.


b) Find the index for LD. What does reversing the letters change?


For LD, the first letter is L, so its weighted contribution is ________. The complete integer code is ________:


C(LD)=31⋅12+4=372+4=376.C(\text{LD})=31\cdot12+4=372+4=376.


Reduce this code using 11⋅34=37411\cdot34=374:


h(LD)=376 mod 11=376−374=2.h(\text{LD})=376\bmod11=376-374=2.


The index for LD is 2. Reversing the letters changes which value receives the factor of 31. Here, both the integer code and the final index change. Position weighting makes the function sensitive to order, although it does not guarantee different indices for every reversed pair.


c) Find the index for DW. Does sharing an index with DL make the function invalid?


For DW, the integer code is ________:


C(DW)=31⋅4+23=124+23=147.C(\text{DW})=31\cdot4+23=124+23=147.


Using 11⋅13=14311\cdot13=143, reduce the code:


h(DW)=147 mod 11=147−143=4.h(\text{DW})=147\bmod11=147-143=4.


The index for DW is 4, and this does not make the function invalid. A collision occurs when distinct keys receive the same hash output. DL and DW are different keys, yet both receive index 4 because their integer codes differ by exactly 11.


The function remains deterministic, and repeated equal keys still produce equal results. A matching index does not establish key equality. Reducing many possible keys to eleven indices allows distinct keys to share an output.

An inventory program uses strings as keys and defines equality by identical character sequences. Its fixed hash function returns one of 64 indices. Which requirement is necessary for hashing to respect this equality rule?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A key-processing system first produces an integer and then converts it to a valid array position. Match each term with the description that best identifies its role or property.

Match each term
A.

Repeated evaluations with the same input and unchanged settings producing the same result.

B.

Two distinct inputs producing the same output from the function under discussion.

C.

An integer produced from a key before any separate conversion to an array position.

D.

The conversion of an integer into one of the permitted array positions.

Collision

Hash code

Range reduction

Determinism

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A parcel sorter hashes a three-digit label by processing its digits from left to right. Start with a0=0a_0=0. For each digit, use its numeric value djd_j in


aj=(7aj−1+dj) mod 13.a_j=(7a_{j-1}+d_j)\bmod 13.


Here, modulo returns the nonnegative remainder, and all arithmetic is exact. What final index does the label 827 receive? Give an exact integer with no units, to zero decimal places.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice