Hashing: 32 MCQs with answers
All 32 multiple-choice questions from this kit, free and with no sign-up.
Warm-up · 22 questions
Q1. What is the primary purpose of a hash function in a hash table?
BeginnerShow answer & explanation
To map keys to indices in an array
A hash function computes an index from a key, enabling O(1) average-time access in a hash table.
Q2. Which of the following is a common hash function for integer keys?
BeginnerShow answer & explanation
key % table_size
The modulo operation (key % table_size) is a simple and widely used hash function to map keys into the range of table indices.
Q3. What is a collision in hashing?
BeginnerShow answer & explanation
Two different keys map to the same hash index
A collision occurs when a hash function produces the same index for two or more distinct keys.
Q4. Which collision resolution technique uses a linked list at each table slot?
BeginnerShow answer & explanation
Separate chaining
Separate chaining stores all elements that hash to the same index in a linked list (or similar structure) at that slot.
Q5. Which of the following is a common application of hashing?
BeginnerShow answer & explanation
Database indexing
Hashing is widely used in database indexing, symbol tables, caches, and many other applications requiring fast lookups.
Q6. A hash function can map two different keys to the same index.
BeginnerShow answer & explanation
True
Yes, this is called a collision, which is inevitable when the number of possible keys exceeds the table size.
Q7. In a hash table, the order of elements is preserved as they are inserted.
BeginnerShow answer & explanation
False
Hash tables do not preserve insertion order; elements are placed based on their hash values, not their insertion sequence.
Q8. What is the primary purpose of a hash function in a hash table?
BeginnerShow answer & explanation
To map keys to array indices
A hash function's main role is to convert a key into an array index, enabling direct access to the table slot.
Q9. Which of the following is a common hash function for integer keys?
BeginnerShow answer & explanation
Key modulo table size ($k \mod m$)
The modulo operation ($k \mod m$) is a standard and simple hash function for integer keys, producing an index in the range $[0, m-1]$.
Q10. What is a collision in hashing?
BeginnerShow answer & explanation
When two different keys map to the same index
A collision occurs when a hash function maps two distinct keys to the same array index, requiring a resolution strategy.
Q11. Which collision resolution technique uses a linked list at each table slot?
BeginnerShow answer & explanation
Separate chaining
Separate chaining stores all elements that hash to the same index in a linked list at that slot.
Q12. In open addressing, when a collision occurs, what does the algorithm do?
BeginnerShow answer & explanation
It probes alternative slots until an empty one is found
Open addressing resolves collisions by searching for the next available slot using a probe sequence, unlike chaining which uses extra memory outside the table.
Q13. What is the load factor (α) of a hash table?
BeginnerShow answer & explanation
The ratio of the number of stored elements to the table size
The load factor $\alpha = n/m$ (where $n$ is the number of keys and $m$ is the table size) indicates how full the table is, affecting performance.
Q14. Which of the following is a characteristic of a good hash function?
BeginnerShow answer & explanation
It distributes keys uniformly across the table
A good hash function minimizes collisions by spreading keys evenly over the table, leading to near O(1) average operations.
Q15. What is the average time complexity for search in a well-designed hash table?
BeginnerShow answer & explanation
O(1)
With a good hash function and low load factor, the average search time is constant, O(1), independent of the number of elements.
Q16. In linear probing, if slot h(k) is occupied, where is the next slot tried?
BeginnerShow answer & explanation
h(k) + 1 (mod table size)
Linear probing checks consecutive slots: $(h(k) + i) \mod m$ for $i = 1, 2, \ldots$, so the next slot is $h(k)+1$ modulo the table size.
Q17. What is the main disadvantage of separate chaining?
BeginnerShow answer & explanation
It requires extra memory for pointers or linked lists
Separate chaining uses additional memory to store linked list nodes, which can be a drawback compared to open addressing that uses the table itself.
Q18. What is the primary operation that a hash function performs in a hash table?
BeginnerShow answer & explanation
It maps keys to array indices.
A hash function takes a key and computes an array index where the corresponding value is stored. This mapping enables average O(1) access.
Q19. Which of the following is a common collision resolution technique in hashing?
BeginnerShow answer & explanation
Separate chaining
Separate chaining is a collision resolution method where each table slot holds a linked list of keys that hash to the same index. Other options are unrelated to collision resolution.
Q20. What is the average time complexity for insert, delete, and search operations in a well-designed hash table?
BeginnerShow answer & explanation
O(1)
A well-designed hash table with a good hash function and appropriate load factor achieves average constant time O(1) for insert, delete, and search operations.
Q21. In the context of hashing, what does the term 'load factor' (α) represent?
BeginnerShow answer & explanation
The ratio of the number of stored elements to the table size
The load factor α is defined as the number of elements stored divided by the table size. It is a key indicator of how full the table is and affects performance.
Q22. Which of the following is a common application of hashing?
BeginnerShow answer & explanation
Implementing a symbol table in a compiler
Hashing is widely used in symbol tables (e.g., in compilers), databases, and caches to provide fast key-based lookup. Sorting, tree traversal, and graph algorithms are not primary applications of hashing.
Building up · 7 questions
Q23. In open addressing, when a collision occurs, what does the algorithm do?
IntermediateShow answer & explanation
It probes for the next available slot
Open addressing resolves collisions by probing (e.g., linear, quadratic, or double hashing) to find another empty slot within the same array.
Q24. What is the load factor (α) of a hash table?
IntermediateShow answer & explanation
Number of keys divided by number of slots
Load factor α = n/m, where n is the number of keys stored and m is the number of slots. It indicates how full the table is.
Q25. Which of the following is a characteristic of a good hash function?
IntermediateShow answer & explanation
It distributes keys uniformly across the table
A good hash function minimizes collisions by spreading keys uniformly over the available indices.
Q26. What is the average time complexity for search in a well-designed hash table?
IntermediateShow answer & explanation
O(1)
With a good hash function and low load factor, hash table operations achieve constant average time complexity, O(1).
Q27. In linear probing, if slot h(k) is occupied, where is the next slot tried?
IntermediateShow answer & explanation
h(k) + 1
Linear probing checks consecutive slots: (h(k) + i) mod m for i = 0, 1, 2, ... until an empty slot is found.
Q28. What is the main disadvantage of separate chaining?
IntermediateShow answer & explanation
It requires extra memory for pointers
Separate chaining uses linked lists, which require additional memory for node pointers, unlike open addressing which stores all elements in the array itself.
Q29. Which of the following is NOT a method of open addressing?
IntermediateShow answer & explanation
Separate chaining
Separate chaining is a different collision resolution technique that uses linked lists, not open addressing.
Topper level · 3 questions
Q30. What is the primary reason for choosing a prime number as the table size in modulo-based hashing?
AdvancedShow answer & explanation
It reduces the number of collisions
A prime table size helps distribute keys more uniformly, especially when keys have patterns, reducing clustering and collisions.
Q31. In double hashing, the probe sequence is determined by:
AdvancedShow answer & explanation
Two different hash functions
Double hashing uses a second hash function to compute the step size, making the probe sequence more flexible and reducing clustering.
Q32. What is the worst-case time complexity for search in a hash table with separate chaining?
AdvancedShow answer & explanation
O(n)
In the worst case, all keys collide into one chain, making the search linear in the number of keys, O(n).
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Exam prep
Get Veda free See what students get
Free to start · No ads · Works in 42 languages