Hash Maps and Sets: Foundations and Applications Quiz

Test your understanding of hash maps and sets, including lookups, frequency counting, and key operations. This quiz is perfect for those who want to strengthen their basic knowledge on hash-based data structures and their common uses.

  1. Basic definition

    Which data structure provides fast access to values using unique keys, making it ideal for quick lookups?

    1. Queue
    2. Array
    3. Hash map
    4. Tuple

    Explanation: A hash map allows for fast access to values using unique keys by applying a hashing function, which speeds up lookups. Queues and arrays require searching through elements, making lookups slower for large sets of data. A tuple is an immutable sequence and does not support key-based access. Only hash maps offer direct, efficient key-value access.

  2. Set uniqueness

    What property does a set guarantee when storing elements like names or numbers?

    1. No duplicate elements
    2. Ordered sequence
    3. Unique values for each key
    4. String-only elements

    Explanation: Sets inherently guarantee that no duplicate elements are stored; each element can appear only once. Ordered sequence is not guaranteed by sets, and unique values for each key refers instead to hash maps. Sets can also store various types, not just strings, so 'string-only elements' is incorrect.

  3. Key usage

    In a hash map storing student IDs and their grades, what would be the typical key?

    1. Student ID
    2. Number of records
    3. Table name
    4. Grade

    Explanation: The key in this scenario is the student ID because it uniquely identifies each student and directly maps to their grade. 'Grade' would usually be a value, not a key. The number of records and table name are unrelated to individual entries and do not function as keys in a hash map.

  4. Frequency counting

    Which data structure is most efficient for counting how many times each word appears in a list of words?

    1. Linked list
    2. Stack
    3. Tree set
    4. Hash map

    Explanation: A hash map is best suited for counting occurrences because you can use each word as a key and increment its value each time it appears. A stack and a linked list do not have direct lookup capabilities needed for counting. A tree set does not store counts, only unique elements.

  5. Set membership

    What operation does a set handle most efficiently compared to a list: checking if an element exists?

    1. Sometimes
    2. Yes
    3. No
    4. Never

    Explanation: Sets provide efficient element membership checks, typically in constant time, while lists require scanning each item, making them slower for this purpose. 'No', 'Sometimes', and 'Never' are incorrect because sets are explicitly designed for fast membership verification.

  6. Hash map values

    If you want to store the price of multiple products, which data structure allows you to associate each product's name directly with its price?

    1. Hash map
    2. Array
    3. Circular buffer
    4. Priority queue

    Explanation: A hash map lets you associate (or map) a product name (the key) to its price (the value) in a direct and efficient way. Priority queues, arrays, and circular buffers do not support direct key-value association as intended in this example.

  7. Removing duplicates

    If you have a list with repeated numbers and want a collection with each number appearing only once, which structure should you use?

    1. Queue
    2. Stack
    3. Set
    4. Dictionary

    Explanation: A set automatically stores only unique elements, removing duplicates from the list. Queues and stacks may retain duplicates, and while 'dictionary' (or hash map) could enforce uniqueness in keys, it is not primarily used for deduplication like sets are.

  8. Lookup speed

    Why are hash maps generally preferred over lists for searching for a specific key's value?

    1. Because lists always use more memory
    2. Because hash maps provide faster lookups via hashing
    3. Because lists can only store numbers
    4. Because hash maps can't have any duplicate values

    Explanation: Hash maps use a hash function to locate keys quickly, often in constant time, making them preferable for search tasks compared to the linear search required in lists. Lists can store various types, not just numbers, and 'no duplicate values' and 'more memory' are not core reasons for their difference in lookup efficiency.

  9. Set modification

    What happens if you try to add an element to a set that is already present?

    1. The set becomes unordered
    2. The set stays the same
    3. An error occurs
    4. A duplicate is added

    Explanation: Adding an existing element to a set has no effect; the set remains unchanged because it only stores unique elements. Sets do not add duplicates, and such an operation does not cause an error or a change in the set's orderliness (sets are unordered by design).

  10. Invalid key types

    Which of the following is typically not allowed as a hash map key?

    1. An integer
    2. A tuple of numbers
    3. A mutable list
    4. A string

    Explanation: Most hash maps require keys to be immutable and hashable; a mutable list can change and so cannot be hashed reliably. Integers, strings, and tuples (if their elements are hashable) are all acceptable key types in most implementations, so these other options are permitted.