Test your understanding of hash maps and sets with these beginner-friendly questions covering constant-time lookups, frequency counting, and grouping elements like deduplication and finding anagrams. This quiz helps you reinforce key concepts and practical usage of hash-based data structures.
Which operation on a hash map typically has constant-time complexity when retrieving the value for a given key?
Explanation: Lookups in a hash map are generally O(1) or constant time because hashing provides direct access to the value using the key. Sorting is not related to hash map operations; it's a separate algorithm with higher complexity. Traversal refers to visiting all elements, which takes linear time. Insertion sort is an algorithm and not a hash map operation.
What is the primary reason to use a set data structure when you want to store values?
Explanation: Sets enforce uniqueness, ensuring that no duplicate elements exist within them. Keeping elements sorted is a property of some other data structures like trees, not sets. Counting occurrences is typically handled by maps or specialized counters. Mapping keys to values describes a hash map, not a set.
When counting how many times each word appears in a list of words, which data structure is most suitable?
Explanation: A hash map allows you to associate each word with the number of times it appears, making frequency counting efficient. Queues and stacks are linear data structures unsuitable for counting occurrences. A heap is used for prioritizing elements, not direct frequency counting.
Given a list of numbers, which simple approach uses a set to check if any number appears more than once?
Explanation: By adding each number to a set and checking if it already exists, you can efficiently detect duplicates. Sorting and checking adjacent elements works but is less direct. Using a counter could work, but is more suited for keeping track of frequencies. Finding the maximum and minimum does not help in detecting duplicates.
Which hash map strategy helps group words that are anagrams of each other, such as ['bat', 'tab', 'cat']?
Explanation: By sorting each word and using the sorted version as a key in a hash map, you can group all anagrams together. Mapping by word lengths is insufficient, as non-anagrams may share lengths. Counting vowels ignores the other letters. Sorting the entire list arranges words but does not guarantee proper anagram grouping.
What advantage do sets offer when checking if an item has already been seen in large data sets?
Explanation: Sets use hashing to provide quick checking for whether an item exists, making membership testing fast. They do not sort their elements automatically. Sets do not store duplicates by design, and 'resizing analytically' is not a standard concept associated with sets.
Which method quickly removes duplicate items from a list of numbers?
Explanation: Converting a list to a set automatically drops duplicates due to the set's uniqueness property; converting back gives a deduplicated list. Repeating the list or reversing it doesn't affect duplicates. Counting items only tells you the length, not uniqueness.
If a hash map receives two entries with the same key but different values, what happens?
Explanation: In a hash map, the value for a key is replaced if that key is added again, so only the latest value remains. Hash maps do not store both values as pairs. Unlike lists, hash maps require unique keys; an error is not thrown, and duplicate keys do not exist as separate entries.
Which real-world scenario best illustrates when to use a hash map?
Explanation: A hash map excels at associating unique keys, like student IDs, with values such as grades, for quick retrieval. Keeping order is best handled by lists or queues. Recursion is a programming technique, not a storage structure. A queue is used to maintain processing order, not key-value mapping.
When two keys hash to the same slot in a hash map, what is this situation called?
Explanation: When two keys produce the same hash index, it's known as a collision, and hash maps have mechanisms to handle it. 'Confusion' is not an appropriate technical term here. 'Stacking' and 'overload' refer to different computer science concepts unrelated to hashing conflicts.