Question 1
EasyWhat is the average time complexity of searching for a key in a hash table?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the average time complexity of searching for a key in a hash table?
O(n)
O(1)
O(log n)
O(n log n)
Answer: B
A good hash function spreads keys evenly, so each lookup checks only a few entries on average.
What is a hash collision?
Two equal keys
A key that can't be hashed
A full hash table
Two different keys hashing to the same index
Answer: D
Collisions are unavoidable when there are more possible keys than slots.
Which collision-resolution technique stores colliding keys in a linked list at each slot?
Separate chaining
Linear probing
Double hashing
Quadratic probing
Answer: A
Probing methods (open addressing) instead look for another empty slot in the table.
Which Python data structure is implemented as a hash table?
dict
tuple
str
list
Answer: A
Python dicts (and sets) use hashing for O(1) average lookups.
What is the output of this code?
freq = {}
for ch in "aba":
freq[ch] = freq.get(ch, 0) + 1
print(freq)
{'a': 3}
{'b': 1, 'a': 2}
Answer: D
The loop counts each character: a appears twice and b once.
What is the load factor of a hash table?
Number of stored keys ÷ number of slots
Number of collisions
Number of slots ÷ number of keys
Size of each key
Answer: A
A higher load factor means more collisions; tables usually resize when it passes a threshold such as 0.75.
Which problem is a classic use of a hash set?
Balancing a tree
Checking whether an array contains duplicates in O(n)
Sorting an array
Finding the shortest path
Answer: B
Add each element to a set; if it is already there, you have found a duplicate.
What does a hash function do in a hash table?
Encrypts the values
Deletes old keys
Turns a key into an index (slot number) in the table
Sorts the keys
Answer: C
The key's hash decides where its entry is stored.
Which property makes a hash function good for a hash table?
It is very slow
It always returns 0
It spreads keys evenly across the slots
It returns the key unchanged
Answer: C
An even spread keeps collisions, and so lookup times, low.
In open addressing, what is linear probing?
Storing colliding keys in a linked list
Rehashing the whole table every time
Ignoring the colliding key
After a collision, checking the next slots one by one until a free one is found
Answer: D
With h(k) taken, try h(k) + 1, h(k) + 2, … (wrapping round).
{'a': 1, 'b': 1}
{'a': 2, 'b': 1}