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.
Identifying Benefits
Which of the following best describes why a hash map is useful for quickly checking if an item exists in a collection?
- It allows constant-time lookups on average.
- It compresses all values to save memory.
- It automatically duplicates keys when inserting new data.
- It requires linear-time searching for elements.
- It always stores items in sorted order.
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?
- {2, 6, 8}
- {2, 4, 6, 8}
- {2, 6, 8, 10}
- {2, 4, 6, 8, 4}
- {2, 4, 4, 6, 8}
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?
- Array list
- Hash map
- Stack
- Queue
- Priority tree
Key Uniqueness
In a hash map, what happens if you try to insert a key that already exists?
- The existing value for the key is replaced with the new value.
- Both values for the key are stored in a list.
- A random value is picked between the two.
- The key is ignored and no changes are made.
- An error occurs and the program stops.
Case Sensitivity
When counting the frequency of words using a hash map, how will the words 'Apple' and 'apple' be treated by default?
- As the same key
- They will be removed automatically
- As invalid entries
- As a numeric key and a string key
- As two different keys
Set Use Case
Which scenario is best suited for using a set instead of a hash map?
- Mapping cities to their populations
- Storing pairs of products and prices
- Checking for duplicate entries in a list
- Counting letter frequencies in a word
- Sorting numbers in ascending order
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?
- Set its value to null
- Initialize its value to 0 before incrementing
- Ignore the item and move to the next
- Multiply the value by 2
- Delete the key immediately
Collision Explanation
If two different keys produce the same hash in a hash map, this event is called what?
- Convolution
- Confusion
- Concatenation
- Conduction
- Collision
Membership Test
Given a set of banned usernames, how would you best check if the username 'guest123' is not allowed?
- Convert the set to a list and use binary search
- Sort the set and look through each username
- Count the number of vowels in 'guest123'
- Check if 'guest123' is contained in the set
- Check the username against regular expressions only
Deleting Entries
Which action removes a key and its value from a hash map?
- Add the key to the set
- Delete the key from the hash map
- Set the key’s value to zero
- Insert the key again with a new value
- Sort the keys alphabetically