Essential Concepts in Hash Maps and Sets Quiz

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.

  1. Hash Map Basics

    Which operation on a hash map typically has constant-time complexity when retrieving the value for a given key?

    1. Lookup
    2. Sorting
    3. Insertion sort
    4. Traversal

    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.

  2. Function of a Set

    What is the primary reason to use a set data structure when you want to store values?

    1. To ensure all elements are unique
    2. To keep elements in sorted order
    3. To count occurrences of items
    4. To map keys to 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.

  3. Frequency Counting

    When counting how many times each word appears in a list of words, which data structure is most suitable?

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

    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.

  4. Detecting Duplicates

    Given a list of numbers, which simple approach uses a set to check if any number appears more than once?

    1. Use a counter to increment for each repeat
    2. Sort the list and check adjacent elements
    3. Add each number to a set and check for existence before adding
    4. Find the maximum and minimum

    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.

  5. Anagram Grouping

    Which hash map strategy helps group words that are anagrams of each other, such as ['bat', 'tab', 'cat']?

    1. Sort the original list only
    2. Count vowels in words
    3. Map word lengths to words
    4. Use sorted word as a key

    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.

  6. Constant-Time Membership Test

    What advantage do sets offer when checking if an item has already been seen in large data sets?

    1. Fast membership tests via hashing
    2. Guaranteed storage of duplicate values
    3. Automatic sorting of elements
    4. Resizing analytically

    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.

  7. Removing Duplicates

    Which method quickly removes duplicate items from a list of numbers?

    1. Convert the list into a set and back to a list
    2. Count total number of items
    3. Repeat the list twice
    4. Reverse the list

    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.

  8. Key Uniqueness in Hash Maps

    If a hash map receives two entries with the same key but different values, what happens?

    1. Both keys remain separate
    2. Both values are stored as a pair
    3. The most recent value replaces the previous one
    4. An error is thrown

    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.

  9. Prime Use Case for Hash Map

    Which real-world scenario best illustrates when to use a hash map?

    1. Performing a recursive search
    2. Storing and retrieving a student's grade by their unique ID
    3. Maintaining a queue of printing tasks
    4. Keeping track of the order in which students arrive

    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.

  10. Handling Collisions

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

    1. Stacking
    2. Confusion
    3. Overload
    4. Collision

    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.