Hash Maps and Sets Fundamentals Quiz Quiz

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.

  1. Instant Lookups

    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?

    1. Hash maps provide constant-time lookup for keys on average.
    2. Hash maps compress data more efficiently.
    3. Hash maps automatically order their keys alphabetically.
    4. Hash maps sort values for fast searching.

    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.

  2. Counting Occurrences

    If you want to find how many times each word appears in a document, which data structure is most suitable?

    1. Stack
    2. Heap
    3. Queue
    4. Hash map

    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.

  3. Removing Duplicates

    Which data structure lets you quickly remove all duplicate values from a list of numbers while keeping only unique items?

    1. Buffer
    2. Array
    3. Tree
    4. Set

    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.

  4. Classic Problem Application

    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?

    1. By using a queue to cycle through all possible pairs
    2. By tracking previously seen values for immediate lookup of a complement
    3. By grouping numbers by their parity (even or odd)
    4. By storing data in sorted order for binary search

    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.

  5. Detecting Anagrams

    What is a practical way to use hash maps to check if two words, like 'listen' and 'silent', are anagrams?

    1. Sort both words alphabetically before comparing
    2. Check word lengths only
    3. Count each letter’s frequency in both words and compare using hash maps
    4. Use a stack to reverse both words first

    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.

  6. Time Complexity Fundamentals

    What is the average-case time complexity for looking up a key in a hash map?

    1. O(1)
    2. O(log n)
    3. O(n log n)
    4. O(n)

    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.

  7. Handling Collisions

    When two keys hash to the same location in a hash map, what is this event called?

    1. Duplicate
    2. Permutation
    3. Overflow
    4. Collision

    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.

  8. Membership Test

    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?

    1. It compresses numbers to use less space
    2. It keeps items ordered for faster searching
    3. It randomizes the order of elements
    4. It allows fast membership checks on average

    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.

  9. Key Uniqueness

    Which of the following is always guaranteed in both hash maps and sets?

    1. Each value has multiple aliases
    2. All values are sorted alphabetically
    3. All items are referenced by position
    4. All keys or items stored are unique

    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.

  10. Removing Duplicates from a List

    What is the best way to remove duplicates from a list of email addresses using built-in data structures?

    1. Sort the list and remove items one at a time
    2. Convert the list to a set, then back to a list
    3. Reverse the list before iterating through it
    4. Rotate the list several times

    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.