edited by
27,513 views
106 106 votes

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$?

  1. $h(i) = i^2 \text{mod } 10$
  2. $h(i) = i^3 \text{mod } 10$
  3. $h(i) = (11 \ast i^2) \text{mod } 10$
  4. $h(i) = (12 \ast i^2) \text{mod } 10$

5 Answers

Best answer
152 152 votes

Since mod $10$ is used, the last digit matters.

If we CUBE all numbers from $0$ to $9$, we get the following

$\begin{array}{ccc} \textbf{Number} & \textbf{Cube} & \textbf{Last Digit in Cube} \\  \text{0} & \text{0} & \text{0} \\  \text{1} & \text{1} & \text{1} \\  \text{2} & \text{8} & \text{8} \\  \text{3} & \text{27} & \text{7} \\  \text{4} & \text{64} & \text{4} \\ \text{5} & \text{125} & \text{5} \\  \text{6} & \text{216} & \text{6} \\  \text{7} & \text{343} & \text{3} \\  \text{8} & \text{512} & \text{2} \\  \text{9} & \text{729} & \text{9} \\   \end{array}$

Therefore all numbers from $0$ to $2020$ are equally divided in to $10$ buckets. If we make a table for square, we won't get equal distribution as shown in the following table. $1, 4, 6$ and $9$ are repeated, so these buckets would have more entries and there are no buckets corresponding to $2, 3, 7$ and $8.$

$\begin{array}{ccc} \textbf{Number} & \textbf{Square} & \textbf{Last Digit in Cube} \\  \text{0} & \text{0} & \text{0} \\  \text{1} & \text{1} & \textbf{1} \\  \text{2} & \text{4} & \textbf{4} \\  \text{3} & \text{9} & \textbf{9} \\  \text{4} & \text{16} & \textbf{6} \\ \text{5} & \text{25} & \text{5} \\  \text{6} & \text{36} & \textbf{6} \\  \text{7} & \text{49} & \textbf{9} \\  \text{8} & \text{64} & \textbf{4} \\  \text{9} & \text{81} & \textbf{1} \\   \end{array}$

http://geeksquiz.com/gate-gate-cs-2015-set-2-question-43/

Correct Answer: $B$

edited by
84 84 votes

This is aptitude question, actually! (concept of power cycle will be helpful)

a) (0,1,4,9,6,5,6,9,4,1,0) repeated [Not including 2,3,7,8] (loss of 4)

b) (0,1,8,7,4,5,6,3,2,9) repeated [Includes everyone]

c) (0,1,4,9,6,5,6,9,4,1,0) repeated [Not including 2,3,7,8] (loss of 4)

d) (0,2,4,6,8) repeated [Not inluding any odd nos.](loss of 5)

Answer is (B)

12 12 votes

Ans B, all numbers are generated in this, if you have any doubt check this 

2 2 votes
We are given four candidate hash functions and asked to determine which one distributes the keys $i = 0, 1, 2, \dots, 2020$ most uniformly across 10 buckets labeled $0$ through $9$. A uniform distribution means that each bucket receives approximately the same number of keys. Since the total number of keys is $2021$, an ideal hash function would assign either $\lfloor 2021/10 \rfloor = 202$ or $\lceil 2021/10 \rceil = 203$ keys to each bucket.

Because all candidate functions are defined modulo $10$, their behavior is periodic with period $10$. Thus, it suffices to examine the output of each function for $i = 0$ to $9$; the pattern will repeat every 10 inputs. Over $2021$ keys, there are $202$ complete cycles of $10$ (covering $i = 0$ to $2019$) and one extra value ($i = 2020$). Therefore, uniformity is determined by how many distinct residues modulo $10$ each function produces and whether those residues are equally frequent within one period.

We now analyze each option.

Option A: $h(i) = i^2 \mod 10$

Compute $i^2 \mod 10$ for $i = 0$ to $9$:

\[
\begin{array}{|c|c|c|}
\hline
i & i^2 & i^2 \mod 10 \\
\hline
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 \\
\hline
\end{array}
\]

 

The set of residues is ${0, 1, 4, 5, 6, 9}$ — only 6 distinct buckets are used. Buckets $2, 3, 7, 8$ never receive any keys. Hence, the distribution is highly non-uniform.

Option B: $h(i) = i^3 \mod 10$

Compute $i^3 \mod 10$ for $i = 0$ to $9$:

\[
\begin{array}{|c|c|c|}
\hline
i & i^3 & i^3 \mod 10 \\
\hline
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 \\
\hline
\end{array}
\]
 

The residues are ${0, 1, 2, 3, 4, 5, 6, 7, 8, 9}$ — all 10 buckets appear exactly once in each period of 10. Therefore, over 202 full periods, each bucket receives exactly 202 keys. The final key ($i = 2020$) satisfies $2020 \equiv 0 \pmod{10}$, so $h(2020) = 0^3 \mod 10 = 0$, giving bucket $0$ one extra key (total 203). All other buckets have 202 keys. This is as uniform as possible.

Option C: $h(i) = (11 \cdot i^2) \mod 10$

Since $11 \equiv 1 \pmod{10}$, we have
$$
h(i) = (11 \cdot i^2) \mod 10 = (i^2) \mod 10.
$$
This is identical to Option A, and thus also uses only 6 buckets. The distribution is non-uniform.

Option D: $h(i) = (12 \cdot i^2) \mod 10$

Note that $12 \equiv 2 \pmod{10}$, so
$$
h(i) = (2 \cdot i^2) \mod 10.
$$
Compute for $i = 0$ to $9$:

\[
\begin{array}{|c|c|c|c|}
\hline
i & i^2 & 2i^2 & (2i^2) \mod 10 \\
\hline
0 & 0 & 0 & 0 \\
1 & 1 & 2 & 2 \\
2 & 4 & 8 & 8 \\
3 & 9 & 18 & 8 \\
4 & 16 & 32 & 2 \\
5 & 25 & 50 & 0 \\
6 & 36 & 72 & 2 \\
7 & 49 & 98 & 8 \\
8 & 64 & 128 & 8 \\
9 & 81 & 162 & 2 \\
\hline
\end{array}
\]

The residues are ${0, 2, 8}$ only 3 buckets are used. This is the least uniform of all options.

Conclusion

Only Option B produces a complete and balanced set of residues modulo $10$, resulting in a near-perfect uniform distribution across all buckets. All other options suffer from significant clustering due to algebraic properties of squaring or scalar multiplication modulo $10$.

$$
\boxed{\text{B. } h(i) = i^3 \mod 10}
$$
0 0 votes

Alternative approach – 
Using the 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. 
Hence Option (C) is correct. answer 

2 flags:
✌ Low quality (Krishna Reddy kyp)
✌ Edit necessary (Sukhdev_Sahu “option B is the correct answer”)
Answer:
Position:
Show:

Related questions

59 59 votes
4 answers 4 answers
24.8k
24.8k views
Arjun asked Feb 12, 2020
24,804 views
Consider a double hashing scheme in which the primary hash function is $h_1(k)= k \text{ mod } 23$, and the secondary hash function is $h_2(k)=1+(k \text{ mod } 19)$. Ass...
82 82 votes
7 answers 7 answers
29.5k
29.5k views
go_editor asked Feb 12, 2015
29,518 views
Consider a complete binary tree where the left and right subtrees of the root are max-heaps. The lower bound for the number of operations to convert the tree to a heap is...
97 97 votes
12 answers 12 answers
26.3k
26.3k views
go_editor asked Feb 12, 2015
26,279 views
A Young tableau is a $2D$ array of integers increasing from left to right and from top to bottom. Any unfilled entries are marked with $\infty$, and hence there cannot be...
52 52 votes
4 answers 4 answers
17.8k
17.8k views
go_editor asked Feb 12, 2015
17,801 views
Consider the C program below#include <stdio.h int *A, stkTop; int stkFunc (int opcode, int val) { static int size=0, stkTop=0; switch (opcode) { case -1: size = val; brea...