Hash Maps and Sets: Understanding Lookups and Frequency Counting Quiz

Test your knowledge of hash maps and sets with questions covering basic lookups, frequency counting, and common use cases. This quiz aims to strengthen your understanding of these foundational data structures.

  1. Identifying Benefits

    Which of the following best describes why a hash map is useful for quickly checking if an item exists in a collection?

    1. It allows constant-time lookups on average.
    2. It compresses all values to save memory.
    3. It automatically duplicates keys when inserting new data.
    4. It requires linear-time searching for elements.
    5. It always stores items in sorted order.
  2. Basic Set Operation

    Suppose you have a set containing the numbers {2, 4, 6, 8}. What will the set contain after adding the number 4 again?

    1. {2, 6, 8}
    2. {2, 4, 6, 8}
    3. {2, 6, 8, 10}
    4. {2, 4, 6, 8, 4}
    5. {2, 4, 4, 6, 8}
  3. Counting Frequencies

    If you want to count the number of times each word appears in a paragraph, which data structure is most efficient for this task?

    1. Array list
    2. Hash map
    3. Stack
    4. Queue
    5. Priority tree
  4. Key Uniqueness

    In a hash map, what happens if you try to insert a key that already exists?

    1. The existing value for the key is replaced with the new value.
    2. Both values for the key are stored in a list.
    3. A random value is picked between the two.
    4. The key is ignored and no changes are made.
    5. An error occurs and the program stops.
  5. Case Sensitivity

    When counting the frequency of words using a hash map, how will the words 'Apple' and 'apple' be treated by default?

    1. As the same key
    2. They will be removed automatically
    3. As invalid entries
    4. As a numeric key and a string key
    5. As two different keys
  6. Set Use Case

    Which scenario is best suited for using a set instead of a hash map?

    1. Mapping cities to their populations
    2. Storing pairs of products and prices
    3. Checking for duplicate entries in a list
    4. Counting letter frequencies in a word
    5. Sorting numbers in ascending order
  7. Default Value Handling

    When incrementing the count of an item in a hash map for the first time, which of the following is a common approach to set a default value?

    1. Set its value to null
    2. Initialize its value to 0 before incrementing
    3. Ignore the item and move to the next
    4. Multiply the value by 2
    5. Delete the key immediately
  8. Collision Explanation

    If two different keys produce the same hash in a hash map, this event is called what?

    1. Convolution
    2. Confusion
    3. Concatenation
    4. Conduction
    5. Collision
  9. Membership Test

    Given a set of banned usernames, how would you best check if the username 'guest123' is not allowed?

    1. Convert the set to a list and use binary search
    2. Sort the set and look through each username
    3. Count the number of vowels in 'guest123'
    4. Check if 'guest123' is contained in the set
    5. Check the username against regular expressions only
  10. Deleting Entries

    Which action removes a key and its value from a hash map?

    1. Add the key to the set
    2. Delete the key from the hash map
    3. Set the key’s value to zero
    4. Insert the key again with a new value
    5. Sort the keys alphabetically