# Hash

Question 1 |

A hash table of length 10 uses open addressing with hash function h(k)=k mod 10, and linear probing. After inserting 6 values into an empty hash table, the table is as shown below.

Which one of the following choices gives a possible order in which the key values could have been inserted in the table?

Which one of the following choices gives a possible order in which the key values could have been inserted in the table?

46, 42, 34, 52, 23, 33 | |

34, 42, 23, 52, 33, 46 | |

46, 34, 42, 23, 52, 33 | |

42, 46, 33, 23, 34, 52 |

**Hash**

**Discuss it**

Question 1 Explanation:

See question 2 of http://www.geeksforgeeks.org/data-structures-and-algorithms-set-24/

Question 2 |

How many different insertion sequences of the key values using the hash function

**h(k) = k mod 10**and linear probing will result in the hash table shown below?10 | |

20 | |

30 | |

40 |

**Hash**

**Discuss it**

Question 2 Explanation:

In a valid insertion sequence, the elements 42, 23 and 34 must appear before 52 and 33, and 46 must appear before 33.
Total number of different sequences = 3! x 5 = 30
In the above expression, 3! is for elements 42, 23 and 34 as they can appear in any order, and 5 is for element 46 as it can appear at 5 different places.

Question 3 |

The keys 12, 18, 13, 2, 3, 23, 5 and 15 are inserted into an initially empty hash table of length 10 using open addressing with hash function h(k) = k mod 10 and linear probing. What is the resultant hash table?

A | |

B | |

C | |

D |

**Hash**

**Discuss it**

Question 3 Explanation:

To get the idea of open addressing concept, you can go through below lines from Wikipedia
.
Open addressing, or closed hashing, is a method of collision resolution in hash tables. With this method a hash collision is resolved by probing, or searching through alternate locations in the array (the probe sequence) until either the target record is found, or an unused array slot is found, which indicates that there is no such key in the table. Well known probe sequences include:

*linear probing*in which the interval between probes is fixed--often at 1.*quadratic probing*in which the interval between probes increases linearly (hence, the indices are described by a quadratic function).*double hashing*in which the interval between probes is fixed for each record but is computed by another hash function.Question 4 |

Consider a hash table of size seven, with starting index zero, and a hash function (3x + 4)mod7. Assuming the hash table is initially empty, which of the following is the contents of the table when the sequence 1, 3, 8, 10 is inserted into the table using closed hashing? Note that ‘_’ denotes an empty location in the table.

8, _, _, _, _, _, 10 | |

1, 8, 10, _, _, _, 3 | |

1, _, _, _, _, _,3 | |

1, 10, 8, _, _, _, 3 |

**Hash**

**Discuss it**

Question 4 Explanation:

Please see http://lcm.csa.iisc.ernet.in/dsa/node38.html for closed hashing and probing.
Let us put values 1, 3, 8, 10 in the hash of size 7.
Initially, hash table is empty

- - - - - - - 0 1 2 3 4 5 6The value of function (3x + 4)mod 7 for 1 is 0, so let us put the value at 0

1 - - - - - - 0 1 2 3 4 5 6The value of function (3x + 4)mod 7 for 3 is 6, so let us put the value at 6

1 - - - - - 3 0 1 2 3 4 5 6The value of function (3x + 4)mod 7 for 8 is 0, but 0 is already occupied, let us put the value(8) at next available space(1)

1 8 - - - - 3 0 1 2 3 4 5 6The value of function (3x + 4)mod 7 for 10 is 6, but 6 is already occupied, let us put the value(10) at next available space(2)

1 8 10 - - - 3 0 1 2 3 4 5 6

Question 5 |

Consider a hash table of size seven, with starting index zero, and a hash function (3x + 4)mod7. Assuming the hash table is initially empty, which of the following is the contents of the table when the sequence 1, 3, 8, 10 is inserted into the table using closed hashing? Note that ‘_’ denotes an empty location in the table.

8, _, _, _, _, _, 10 | |

1, 8, 10, _, _, _, 3 | |

1, _, _, _, _, _,3 | |

1, 10, 8, _, _, _, 3 |

**Hash**

**Discuss it**

Question 5 Explanation:

Please see http://lcm.csa.iisc.ernet.in/dsa/node38.html for closed hashing and probing.
Let us put values 1, 3, 8, 10 in the hash of size 7.
Initially, hash table is empty

- - - - - - - 0 1 2 3 4 5 6The value of function (3x + 4)mod 7 for 1 is 0, so let us put the value at 0

1 - - - - - - 0 1 2 3 4 5 6The value of function (3x + 4)mod 7 for 3 is 6, so let us put the value at 6

1 - - - - - 3 0 1 2 3 4 5 6The value of function (3x + 4)mod 7 for 8 is 0, but 0 is already occupied, let us put the value(8) at next available space(1)

1 8 - - - - 3 0 1 2 3 4 5 6The value of function (3x + 4)mod 7 for 10 is 6, but 6 is already occupied, let us put the value(10) at next available space(2)

1 8 10 - - - 3 0 1 2 3 4 5 6

Question 6 |

Given the following input (4322, 1334, 1471, 9679, 1989, 6171, 6173, 4199) and the hash function x mod 10, which of the following statements are true?
i. 9679, 1989, 4199 hash to the same value
ii. 1471, 6171 hash to the same value
iii. All elements hash to the same value
iv. Each element hashes to a different value
(GATE CS 2004)

i only | |

ii only | |

i and ii only | |

iii or iv |

**Hash**

**Discuss it**

Question 6 Explanation:

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

Question 7 |

Consider a hash table with 100 slots. Collisions are resolved using chaining. Assuming simple uniform hashing, what is the probability that the first 3 slots are unfilled after the first 3 insertions?

(97 × 97 × 97)/100 ^{3} | |

(99 × 98 × 97)/100 ^{3} | |

(97 × 96 × 95)/100 ^{3} | |

(97 × 96 × 95)/(3! × 100 ^{3}) |

**Hash**

**GATE-CS-2014-(Set-3)**

**Discuss it**

Question 7 Explanation:

Simple Uniform hashing function is a hypothetical hashing function that evenly distributes items into the slots of a hash table. Moreover, each item to be hashed has an equal probability of being placed into a slot, regardless of the other elements already placed. (Source: https://en.wikipedia.org/wiki/SUHA_%28computer_science%29).

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)

Question 8 |

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) =i ^{2} mod 10 | |

h(i) =i ^{3} mod 10 | |

h(i) = (11 ∗ i ^{2}) mod 10 | |

h(i) = (12 ∗ i) mod 10 |

**Hash**

**GATE-CS-2015 (Set 2)**

**Discuss it**

Question 8 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 9Therefore 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 112 443 994 1665 25 5 6 3667 4998 6449 811

**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) =i^{3}mod 10 covers all the digits from 0 to 9. Option (B) is correct.Question 9 |

Given a hash table T with 25 slots that stores 2000 elements, the load factor α for T is __________

80 | |

0.0125 | |

8000 | |

1.25 |

**Hash**

**GATE-CS-2015 (Set 3)**

**Discuss it**

Question 9 Explanation:

load factor = (no. of elements) / (no. of table slots) = 2000/25 = 80

Question 10 |

Which of the following statement(s) is TRUE?

- 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.

I only | |

II and III only | |

I and III only | |

II only |

**Hash**

**GATE IT 2006**

**Python Data Type**

**Discuss it**

Question 10 Explanation:

Hash function is defined as any function that can be used to map data of arbitrary size of data to a fixed size data.. The values returned by a hash function are called hash values, hash codes, digests, or simply hashes : Statement 1 is correct
Yes, it is possible that a Hash Function maps a value to a same location in the memmory that's why collision occurs and we have different technique to handle this problem : Statement 3 is coorect.
eg : we have hash function, h(x) = x mod 3
Acc to Statement 1, no matter what the value of 'x' is h(x) results in a fixed mapping location.
Acc. to Statement 3, h(x) can result in same mapping mapping location for different value of 'x' e.g. if x = 4 or x = 7 , h(x) = 1 in both the cases, although collision occurs.
This solution is contributed by

**Mohit Gupta**.There are 24 questions to complete.