MCQ on Hash Tables

Hash tables, also known as hash maps, are an essential data structure in computer science. They offer quick access to values based on a key. Here are some questions to test your understanding of this vital data structure.

1. What is the primary use of a hash table?

a) Search for a key
b) Calculate mathematical hashes
c) Store data in a tree format
d) All of the above

Answer:

a) Search for a key

Explanation:

Hash tables are designed to optimize the searching of a value by its key. The other options are not the primary purposes of a hash table.

2. Which of the following best describes a collision in a hash table?

a) Two keys having the same value
b) Two keys hashing to the same index
c) Forgetting to store a key
d) All keys having unique values

Answer:

b) Two keys hashing to the same index

Explanation:

A collision occurs when two different keys produce the same index in a hash table.

3. What is the time complexity of a successful search in a hash table, in the best case scenario?

a) O(1)
b) O(log n)
c) O(n)
d) O(n^2)

Answer:

a) O(1)

Explanation:

Ideally, if there are no collisions and the hash function is perfect, retrieving a value by its key happens in constant time.

4. Which method resolves collisions by inserting the collided item into the next available space?

a) Separate Chaining
b) Linear Probing
c) Quadratic Probing
d) Double Hashing

Answer:

b) Linear Probing

Explanation:

Linear probing resolves collisions by placing the collided item into the next available slot in the hash table.

5. In the context of a hash table, what does the load factor represent?

a) The number of keys
b) The total size of the array
c) The ratio of the number of keys to the array size
d) The number of collisions

Answer:

c) The ratio of the number of keys to the array size

Explanation:

Load factor is used to determine when to resize the hash table. It’s calculated as (number of keys / array size).

6. Which of the following data structures is commonly used for implementing collision resolution in hash tables?

a) Arrays
b) Linked Lists
c) Trees
d) Graphs

Answer:

b) Linked Lists

Explanation:

Linked lists are often used in the “Separate Chaining” method of collision resolution.

7. What’s the main disadvantage of a high load factor in a hash table?

a) Less memory usage
b) Faster search times
c) More collisions
d) More keys can be stored

Answer:

c) More collisions

Explanation:

A high load factor means the hash table is getting filled, increasing the chances of collisions, and thus, slowing down operations.

8. If a hash table has a load factor greater than 1, it means:

a) It’s less than half full
b) It’s more than half full
c) It’s completely full
d) It’s overfull

Answer:

d) It’s overfull

Explanation:

A load factor greater than 1 indicates that the number of entries exceeds the table’s size.

9. Which of the following best describes open addressing?

a) Only one key-value pair can exist per index
b) Multiple key-value pairs can exist per index using linked lists
c) The hash table uses trees for storage
d) Each key is associated with a unique address

Answer:

a) Only one key-value pair can exist per index

Explanation:

Open addressing means that all elements are stored in the hash table array itself, and when collisions occur, we find another slot in the same table.

10. What would be the worst case time complexity for searching an element in a hash table?

a) O(1)
b) O(log n)
c) O(n)
d) O(n^2)

Answer:

c) O(n)

Explanation:

If all keys collide to the same index, the search operation might need to traverse through all of them, making it O(n).

11. Which of the following scenarios would make a hash table perform poorly?

a) Uniform key distribution
b) High number of insert operations
c) High number of delete operations
d) Non-uniform key distribution

Answer:

d) Non-uniform key distribution

Explanation:

Non-uniform key distribution means that many keys hash to the same indices, leading to collisions and poor performance.

12. Rehashing in a hash table means:

a) Calculating the hash value again
b) Deleting all values
c) Creating a larger table and copying values to the new table
d) Clearing all collisions

Answer:

c) Creating a larger table and copying values to the new table

Explanation:

Rehashing involves resizing the hash table (usually doubling its size) and re-inserting the elements to distribute them uniformly.


Hash tables are immensely powerful for optimizing access times. As with any data structure, understanding their underlying principles and potential pitfalls ensures efficient utilization in real-world applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top