Explore key concepts of using hash maps and sets with the C++ Standard Template Library. This quiz covers usage, efficiency, operations, and common pitfalls to help solidify foundational understanding for programmers working with unordered_map and unordered_set containers in C++.
Which container in C++ STL should you use to implement a hash-based key-value map for efficient average-time lookups?
Explanation: unordered_map is a hash-based associative container that provides constant average-time complexity for lookups. map implements a balanced tree, resulting in log-time lookup, while vector does not support key-value associations. set stores only unique values without a value component, making unordered_map the correct choice.
Which property is always true for unordered_set in C++ STL, even if you try to insert identical values multiple times?
Explanation: An unordered_set ensures all elements are unique, and adding a duplicate value has no effect. Duplicate values are explicitly not allowed. While no sorting is guaranteed, that alone does not distinguish unordered_set from other containers, and the uniqueness requirement is always enforced.
What is the underlying data structure most commonly used by unordered_map and unordered_set?
Explanation: Both unordered_map and unordered_set are implemented using hash tables, allowing fast average-time lookups. Binary search trees like those used in map or set offer log-time complexity. Doubly linked lists and heaps are not used as underlying structures in these containers.
What is the correct way to insert the integer 7 into an unordered_set named 'numbers'?
Explanation: The insert member function is used for adding elements to an unordered_set. add, push, and append are not valid member functions for unordered_set. Using anything other than insert will result in a compilation error.
Which method should you use to efficiently check whether key 42 exists in an unordered_map named 'data'?
Explanation: Using find and comparing to end efficiently checks for key existence without modifying the container. The contains function is only available in more recent standards, and exists does not exist as a member. Using operator[] will insert the key if it does not exist, which is inefficient for existence checks.
What is the average time complexity for inserting an element into an unordered_set?
Explanation: Average-time insertion in an unordered_set is O(1) because it uses a hash table. log n complexity is typical for tree-based containers such as set. O(n) and O(n log n) are incorrect, as hash tables optimize for constant average-time insertion.
If you want to use a custom data type as the key in an unordered_map, what must you provide?
Explanation: For custom keys in unordered_map, a hash function and equality operator are needed for storage and lookup. Comparison operators are used in ordered containers, not unordered_map. Copy and move operations affect copying and moving but are not sufficient for key usage.
Which operation directly affects the number of internal buckets in an unordered_set, potentially reducing collisions?
Explanation: The rehash function changes the number of buckets, impacting collision handling efficiency. sort does not affect hash tables as they are unordered. reserve adjusts capacity for elements, not buckets, and compress is not a valid unordered_set member function.
What happens if you insert a new pair with a key already existing in an unordered_map?
Explanation: When a duplicate key is inserted in an unordered_map, the corresponding value is updated. Insertion does not fail, and unordered_map does not store duplicate keys with different values. The old value is replaced, not deleted without replacement.
What is the correct way to remove the value 15 from an unordered_set named 'nums'?
Explanation: The erase function is the correct way to remove elements from an unordered_set. remove, delete, and pop are not valid member functions and will cause compilation errors. Using erase ensures the specified value is removed if it exists.