Hashing — Concept Check
Questions on Hashing. O(1) average lookups power two-sum, anagram detection, and frequency counting — the most commonly used data structure in interviews.
Questions in this quiz
8 questions on Hashing — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer uses a HashMap to store user IDs and their corresponding data. What is the average time complexity for retrieving a user's data by ID?
- AO(1)
- BO(log n)
- CO(n)
- DO(n log n)
2.In Java, what happens if two keys hash to the same index in a HashMap?
- AThe existing key-value pair is replaced
- BThe HashMap throws an exception
- CA collision occurs and is handled via chaining
- DThe new key-value pair is ignored
3.A service is experiencing slow performance due to high memory usage. You suspect a HashMap is the cause. What could be a probable reason?
- AHashMap has too many collisions leading to O(n) operations
- BHashMap is not suitable for storing large data
- CHashMap operations are inherently slow
- DHashMap is using too much space for each entry
4.Which of the following is NOT a characteristic of Hashing?
- AHandles collisions using chaining or open addressing
- BRequires keys to be sorted
- CMaps keys to values using a hash function
- DProvides O(1) average time complexity for lookups
5.You need to count the frequency of words in a document efficiently. Which data structure should you use?
- ATreeMap
- BArrayList
- CLinkedList
- DHashMap
6.A HashMap in Java is used to store employee IDs and names. What is the worst-case time complexity for retrieving an employee's name by ID?
- AO(n)
- BO(n log n)
- CO(1)
- DO(log n)
7.Why might someone incorrectly assume that a HashMap provides O(n) time complexity for all operations?
- ABecause they assume all data structures have linear complexity
- BBecause they believe the hash function is slow
- CBecause they confuse it with a TreeMap
- DBecause they think collisions are frequent
8.A HashMap is used to implement a cache. What is the average time complexity for inserting a new cache entry?
- AO(log n)
- BO(n)
- CO(1)
- DO(n log n)