GATE | GATE-CS-2015 (Set 2) | Question 65

Which one of the following hash functions on integers will distribute keys most uniformly over 10 buckets numbered 0 to 9 for i ranging from 0 to 2020?
(A) h(i) =i2 mod 10
(B) h(i) =i3 mod 10
(C) h(i) = (11 ∗ i2) mod 10
(D) h(i) = (12 ∗ i) mod 10


Answer: (B)

Explanation: Since mod 10 is used, the last digit matters.

If you do cube all numbers from 0 to 9, you get following

Number    Cube    Last Digit in Cube
  0        0              0 
  1        1              1 
  2        8              8 
  3        27             7 
  4        64             4 
  5        125            5 
  6        216            6
  7        343            3
  8        512            2
  9        729            9  

Therefore all numbers from 0 to 2020 are equally divided in 10 buckets. If we make a table for square, we don’t get equal distribution. In the following table. 1, 4, 6 and 9 are repeated, so these buckets would have more entries and buckets 2, 3, 7 and 8 would be empty.



Number   Square     Last Digit in Cube
  0        0              0 
  1        1              1 
  2        4              4 
  3        9              9 
  4        16             6
  5        25             5 
  6        36             6
  7        49             9
  8        64             4
  9        81             1  

Alternative approach –
Using concept of power of cycle:

(a) (0,1,4,9,6,5,6,9,4,1,0) repeated
(b) (0,1,8,7,4,5,6,3,2,9) repeated
(c) (0,1,4,9,6,5,6,9,4,1,0) repeated
(d) (0,2,4,6,8) repeated

So, only h(i) =i3 mod 10 covers all the digits from 0 to 9.
Option (B) is correct.


Quiz of this Question



My Personal Notes arrow_drop_up