Hash data structure - GeeksQuiz

05 Jan, 2020
    -    -    -   -   -   -   -
    0    1   2   3   4   5   6
    1    -    -   -   -   -   -
    0    1   2   3   4   5   6
    1    -    -   -   -   -   3
    0    1   2   3   4   5   6
    1    8    -   -   -   -   3
    0    1   2   3   4   5   6
    1    8   10   -   -   -   3
    0    1   2    3   4   5   6
    -    -    -   -   -   -   -
    0    1   2   3   4   5   6
    1    -    -   -   -   -   -
    0    1   2   3   4   5   6
    1    -    -   -   -   -   3
    0    1   2   3   4   5   6
    1    8    -   -   -   -   3
    0    1   2   3   4   5   6
    1    8   10   -   -   -   3
    0    1   2    3   4   5   6
Hash function given is mod(10).
9679, 1989 and 4199 all these give same hash value i.e 9
1471 and 6171 give hash value 1 
Probability that the first 3 slots are unfilled after the first 3 insertions = 
                (probability that first item doesn't go in any of the first 3 slots)*
                (probability that second item doesn't go in any of the first 3 slots)*
                (probability that third item doesn't go in any of the first 3 slots)

                 = (97/100) * (97/100) * (97/100) 
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? 
h(i) = (12 ∗ i) mod 10 
h(i) = (11 ∗ i2) mod 10 
h(i) =i3 mod 10 
h(i) =i2 mod 10  
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 Square
  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.  
A hash function takes a message of arbitrary length and generates a fixed length code.A hash function takes a message of fixed length and generates a code of variable length.A hash function may give the same hash value for distinct messages.
A hash function takes a message of arbitrary length and generates a fixed length code.
A hash function takes a message of fixed length and generates a code of variable length.
A hash function may give the same hash value for distinct messages.
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Comments
Old Comments
Must Do Coding Questions for Product Based Companies
SQL Query to Convert VARCHAR to INT
How to Update Multiple Columns in Single Update Statement in SQL?
Difference between var, let and const keywords in JavaScript
Array of Objects in C++ with Examples
How to Replace Values in Column Based on Condition in Pandas?
How to Fix: SyntaxError: positional argument follows keyword argument in Python
C Program to read contents of Whole File
Insert Image in a Jupyter Notebook
How to Replace Values in a List in Python?