Test your understanding of hash maps and hash sets, focusing on fast lookups and frequency counting. This quiz challenges your knowledge of core concepts and practical uses related to key-value pair storage and efficient membership checks.
What is the average-case time complexity for retrieving a value by its key in a hash map?
Explanation: Retrieving a value by key in a hash map is typically O(1) on average, thanks to direct addressing via the hash function. The option O(n) suggests linear search, which only happens in worst-case scenarios like excessive collisions. O(log n) is characteristic of balanced trees, not hash maps. O(n^2) is much slower than hash map lookups and applies to some naive algorithms but not hash maps.
Which operation can a hash set perform most efficiently compared to a list: checking if an item already exists?
Explanation: A hash set is specifically designed for efficient membership testing, usually in constant time. Sorting elements is not a feature of sets since they are unordered. Iterating in order is not guaranteed in sets. Concatenation is a list operation, not applicable to sets.
How would you most effectively count how many times each word appears in a list of words?
Explanation: A hash map allows efficient tracking of word counts by incrementing values associated with each word key. Sorting and counting runs is less efficient and more complicated. Using a hash set only answers membership, not frequency. Calculating average word length doesn’t address frequency at all.
If you need to quickly find all unique numbers in a large list of integers, which data structure should you use?
Explanation: A hash set naturally removes duplicates by only allowing unique elements, making it ideal for this task. A stack, queue, or linked list all allow duplicate values and wouldn't efficiently provide unique elements.
What is the most efficient way to check if a list contains any duplicate elements?
Explanation: Adding elements to a hash set and checking for repeats offers a fast, efficient way to detect duplicates. Double iteration is possible but much slower (O(n^2)). Sorting then checking consecutive pairs works, but requires extra time for sorting. Computing an average doesn't help detect duplicates.
What is the primary purpose of a hash function in a hash map?
Explanation: The hash function maps a key to a bucket where the value is stored, enabling quick access. Ordering keys alphabetically is unrelated to hashing. Data compression is not performed by hash functions. Comparing for equality checks values directly, not via hashing.
What happens in a hash map when two different keys produce the same hash value?
Explanation: A collision means two distinct keys land in the same bucket, so both must be managed, often by chaining or another technique. Overwriting would lose data, which hash maps avoid. Errors do not usually occur on collision; instead, they are handled internally. Ignoring both keys would fail to store important data.
What distinguishes a hash set from a hash map?
Explanation: Hash sets are collections of unique keys with no associated values, while hash maps store key-value pairs. Neither data structure is necessarily ordered, so that option is incorrect. Both structures generally prevent duplicate keys. Both can store various key types, so type restriction is incorrect.
What result should you expect if you attempt to look up a key in an empty hash map?
Explanation: If the hash map has no entries, the key can't be found and a null, none, or default is returned, depending on the implementation. Returning a random entry would be incorrect. Infinite loops do not occur with basic lookup. Automatically adding the key with a default value is not standard behavior in most implementations.
In which situation would using a hash set be preferable to using a list?
Explanation: Hash sets are best for ensuring all entries are unique and for fast membership checking. Maintaining order or accessing by index isn't supported by hash sets. Lists allow duplicates and provide indexing, but are not as efficient for membership testing.