You are on page 1of 8

Unit 2: Hashing

1. What is a hash table?

a) A structure that maps values to keys

b) A structure that maps keys to values

c) A structure used for storage

d) A structure used to implement stack and queue

Answer: b

Explanation: A hash table is used to implement associative arrays which


has a key-value pair, so the has table maps keys to values.

2. If several elements are competing for the same bucket in the hash
table, what is it called?

a) Diffusion

b) Replication

c) Collision

d) Duplication

Answer: c

Explanation: In a hash table, if sevaral elements are computing for the


same bucket then there will be a clash among elements. This condition
is called Collision. The Collision is reduced by adding elements to a
linked list and head address of linked list is placed in hash table.

3. What is direct addressing?


a) Distinct array position for every possible key

b) Fewer array positions than keys

c) Fewer keys than array positions

d) Same array position for all keys

Answer: a

Explanation: Direct addressing is possible only when we can afford to


allocate an array that has one position for every possible key.

4. What is the search complexity in direct addressing?

a) O(n)

b) O(logn)

c) O(nlogn)

d) O(1)

Answer: d

Explanation: Since every key has a unique array position, searching


takes a constant time.

5. What is a hash function?

a) A function has allocated memory to keys

b) A function that computes the location of the key in the array

c) A function that creates an array

d) A function that computes the location of the values in the array


Answer: b

Explanation: In a hash table, there are fewer array positions than the
keys, so the position of the key in the array has to be computed, this is
done using the hash function.

6. Which of the following is not a technique to avoid a collision?

a) Make the hash function appear random

b) Use the chaining method

c) Use uniform hashing

d) Increasing hash table size

Answer: d

Explanation: On increasing hash table size, space complexity will


increase as we need to reallocate the memory size of hash table for
every collision. It is not the best technique to avoid a collision. We can
avoid collision by making hash function random, chaining method and
uniform hashing.

7. What is the load factor?

a) Average array size

b) Average key size

c) Average chain length

d) Average hash table length

Answer: c

Explanation: In simple chaining, load factor is the average number of


elements stored in a chain, and is given by the ratio of number of
elements stored to the number of slots in the array.

8. What is simple uniform hashing?

a) Every element has an equal probability of hashing into any of the


slots

b) A weighted probabilistic method is used to hash elements into the


slots

c) Elements has a Random probability of hashing into array slots

d) Elements are hashed based on priority

Answer: a

Explanation: In simple uniform hashing, any given element is equally


likely to hash into any of the slots available in the array.

9. In simple uniform hashing, what is the search complexity?

a) O(n)

b) O(logn)

c) O(nlogn)

d) O(1)

Answer: d

Explanation: There are two cases, once when the search is successful
and when it is unsuccessful, but in both cases, the complexity is
O(1+alpha) where 1 is to compute the hash function and alpha is the
load factor.
10. In simple chaining, what data structure is appropriate?

a) Singly linked list

b) Doubly linked list

c) Circular linked list

d) Binary trees

Answer: b

Explanation: Deletion becomes easier with doubly linked list, hence it is


appropriate.

11. How many different insertion sequences of the key values using the
same hash function and linear probing will result in the hash table
shown above?

A. 10

B. 20

C. 30

D. 40

Ans : C

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.
12. What is the advantage of using a dynamic set in direct addressing?

A. It saves time

B. It saves space

C. It saves both time and space

D. None of the mentioned

Ans: B

Explanation: Using a dynamic set, the size of the array is restricted to


the number of keys, hence saving space.

13. The best technique for handling collision is:

a. Separate chaining

b. Double hashing

c. Linear probing

d. Quadratic probing

Answer: (d)

14. Which one is the most desirable out of these traits of a hash
function?

a. it must cause more collisions

b. it must be easy to implement

c. it must cause less collisions

d. it must occupy less space


Answer: (c)

15. What can be the techniques to avoid a collision?

A. Make the hash function appear random

B. Use the chaining method

C. Use uniform hashing

D. All of the mentioned

Answer: D

Explanation: Making the hash function random is not really a good


choice, although it is considered one of the techniques to avoid
collisions along with chaining and simple uniform hashing.

16. What is simple uniform hashing?

A. Every element has an equal probability of hashing into any of the


slots

B. A-weighted probabilistic method is used to hash elements into the


slots

C. All of the mentioned

D. None of the mentioned

Answer: A

Explanation: In simple uniform hashing, any given element is equally


likely to hash into any of the slots available in the array.

17. In simple chaining, what data structure is appropriate?


A. Singly linked list

B. Doubly linked list

C. Circular linked list

D. Binary trees

Answer: B

Explanation: Deletion becomes easier with a doubly linked list, hence it


is appropriate.

18. What is the advantage of using a dynamic set in direct addressing?

A. It saves time

B. It saves space

C. It saves both time and space

D. None of the mentioned

Answer: B

Explanation: Using a dynamic set, the size of the array is restricted to


the number of keys, hence saving space.

You might also like