Test your understanding of hash maps and sets, including fast lookups, duplicate removal, counting item frequency, and classic algorithmic scenarios like two-sum and anagram detection. This quiz helps reinforce key concepts and practical uses of these powerful data structures for everyday problem-solving.
What is the main reason hash maps are preferred when you need to check if a value exists in a collection quickly, such as looking for a username in a list?
Explanation: Hash maps are ideal for quick value existence checks because they provide average constant-time lookups. Unlike hash maps, ordering and sorting are not their primary strengths, so options referring to automatic ordering or sorting are incorrect. Compression is not a main feature either; that is a separate concept unrelated to hash map functionality. Thus, constant-time lookup is the correct advantage.
If you want to find how many times each word appears in a document, which data structure is most suitable?
Explanation: A hash map efficiently associates each unique word with its frequency, allowing for easy counting. Stacks and queues are for sequential processing and do not map keys to values. A heap is used for priority operations, not for counting frequencies directly. Therefore, the hash map is the best choice here.
Which data structure lets you quickly remove all duplicate values from a list of numbers while keeping only unique items?
Explanation: Sets store only unique elements, so adding items from a list to a set removes duplicates automatically. Arrays can contain duplicates without restriction, while trees and buffers may not guarantee uniqueness or efficient deduplication. Thus, sets are the standard structure for this purpose.
In the Two Sum problem, you need to check if two numbers in an array add up to a target value. How does a hash map help improve the solution?
Explanation: A hash map allows you to instantly check if the complement to reach the target value has already been seen, optimizing the solution. Binary search requires sorted data and is less efficient in this context. Grouping by even/odd does not help solve the two-sum condition. Queues do not provide the direct key-based lookup needed for optimal performance.
What is a practical way to use hash maps to check if two words, like 'listen' and 'silent', are anagrams?
Explanation: Hash maps help by counting letter frequencies, which can be quickly compared to determine if two words are anagrams. Sorting is another valid approach but doesn't use hash maps. Just checking lengths or reversing words is insufficient, as they can match even for non-anagrams. Thus, comparing letter-count hash maps is more precise.
What is the average-case time complexity for looking up a key in a hash map?
Explanation: Hash maps typically provide constant-time, or O(1), lookups on average. O(n) would mean linear time, O(log n) or O(n log n) are associated with tree or sorting operations, not hash map lookups. Therefore, O(1) is the standard expectation for successful searches.
When two keys hash to the same location in a hash map, what is this event called?
Explanation: A collision occurs when two distinct keys produce the same hash and require special handling in the hash map. Duplicates refer to key or value repetition, not hash mapping. Overflow is unrelated and often refers to exceeding data structure limits. Permutation is a different mathematical concept unrelated to hashing.
How does using a set help when checking if an item exists in a collection, like confirming if a number is present in a lottery draw list?
Explanation: Sets provide quick, often constant-time, checks to see if an element is present. They do not order or compress the elements, nor do they randomize their arrangement. Thus, the main benefit relating to membership tests is rapid checking.
Which of the following is always guaranteed in both hash maps and sets?
Explanation: Both hash maps and sets ensure that keys (and all items, in the case of sets) are unique. Sorting or ordering is not a requirement for either structure. Multiple aliases or referencing by position are not inherently guaranteed by these data structures.
What is the best way to remove duplicates from a list of email addresses using built-in data structures?
Explanation: Converting a list to a set keeps only unique items, and converting it back provides a deduplicated list. Sorting and manually removing items is more complex and less efficient. Reversing or rotating does not remove duplicates. Therefore, the set conversion method is the most direct and effective approach.