Top MCQs on Hash Data Strcuture with Answers

Last Updated : 27 Sep, 2023

A hash data structure, often simply referred to as a hash, is a fundamental data structure in computer science. It is designed to efficiently store, retrieve, and manage data by using a special mathematical function called a hash function. The key characteristic of hash data structures is their ability to map keys …More on Hash Data Structure

Hash Data Structure

Hash Data Structure

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?

Cross

46, 42, 34, 52, 23, 33

Cross

34, 42, 23, 52, 33, 46

Tick

46, 34, 42, 23, 52, 33

Cross

42, 46, 33, 23, 34, 52



Question 1-Explanation: 

Here linear probing is used with the hash function h(k)=k mod 10.

Let's analyze all the options carefully:

KeyABCD
0    
1    
242424242
352232333
434343423
523525234
646334646
733463352
8    
9    

After analyzing all the options (C) is the correct possible order in which the key values could have been inserted in the table

(A) doesn’t create the hash table as the element 52 appears before 23 in this sequence.
(B) doesn't create the hash table as the element 33 appears before 46 in this sequence.
(C)create the hash table as the element 42, 23 and 34 appear before 52 and 33, and 46 appears before 33.
(D)doesn't create the hash table as element 33 appears before 23 in this sequence.

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? 

 

Cross

10

Cross

20

Tick

30

Cross

40



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?

 

Cross

A

Cross

B

Tick

C

Cross

D



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

Which of the following hash functions is most likely to cause clustering in a hash table?

Tick

h(k) = k % m

Cross

h(k) = floor(m * (kA mod 1))

Cross

h(k) = k

Cross

h(k) = ((k / m) + k * m) + k % m



Question 4-Explanation: 

The modulo operation in option a) results in clustering since the hash values of keys that are close to each other will also be close, leading to collisions. Options B, C, and D use more complex hash functions, which spread out the keys more uniformly across the table..

Question 5

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?

Tick

(97 × 97 × 97)/1003

Cross

(99 × 98 × 97)/1003

Cross

(97 × 96 × 95)/1003

Cross

(97 × 96 × 95)/(3! × 1003)



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

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 6

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?
 

Cross

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

Cross

h(i) = (11 ∗ i2) mod 10
 

Tick

h(i) =i3 mod 10
 

Cross

h(i) =i2 mod 10 
 



Question 6-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 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 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 



 

Question 7

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

Tick

80

Cross

0.0125

Cross

8000

Cross

1.25



Question 7-Explanation: 

load factor = (no. of elements) / (no. of table slots) = 2000/25 = 80.
Hence Option(A) is the correct answer.

Question 8

Which of the following statement(s) is TRUE?

  1. A hash function takes a message of arbitrary length and generates a fixed length code.
  2. A hash function takes a message of fixed length and generates a code of variable length.
  3. A hash function may give the same hash value for distinct messages.
Cross

I only

Cross

II and III only

Tick

I and III only

Cross

II only



Question 8-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 memory that\'s why collision occurs and we have different technique to handle  this problem : Statement 3 is correct. 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.   

Question 9

Consider a hash function that distributes keys uniformly. The hash table size is 20. After hashing of how many keys will the probability that any new key hashed collides with an existing one exceed 0.5.

Cross

5

Cross

6

Cross

7

Tick

10



Question 9-Explanation: 

For each entry probability of collision is 1/20 {as possible total spaces =20, and an entry will go into only 1 place}
Say after inserting x values probability becomes 1/2
=> (1/20).x = 1/2
=> X=10

Hence Option(D) is the correct answer.

Question 10

What is the probability of a collision when hashing n keys into a hash table of size m, assuming that the hash function produces a uniform random distribution?

Cross

O(1/n)

Tick

O(n/m)

Cross

O(logn)

Cross

O(m/n)



Question 10-Explanation: 

The probability of a collision occurring is dependent on the number of items hashed (n) and the size of the hash table (m). As the number of items increases, the probability of a collision also increases. However, as the size of the hash table increases, the probability decreases. Therefore, the probability of a collision can be estimated as O(n/m).

There are 30 questions to complete.


Share your thoughts in the comments

Similar Reads