Key Lookup Time
Which time complexity best describes searching for a value by key in a well-implemented hash map (dictionary) with minimal collisions?
- O(1)
- O(n)
- O(log n)
- O(n log n)
- O(2^n)
Duplicate Keys
In a hash map or dictionary, what happens if you insert a new value with a key that already exists?
- The old value is overwritten by the new value
- Both values are stored under the same key
- An error is thrown
- A warning is issued but both values are kept
- The new value is discarded
Python Hash Map Syntax
In Python, which syntax correctly accesses the value associated with the key 'age' in the dictionary person = {'name': 'Sam', 'age': 30}?
- person['age']
- person.age
- person-u003E'age'
- person.age()
- person['Age']
Dictionary Keys
Which of the following can NOT be used as a key in a Python dictionary?
- A list
- A tuple
- An integer
- A string
- A floating-point number
Checking Key Existence
Suppose data = {'a': 1, 'b': 2, 'c': 3}. What’s the most efficient way to check if 'b' exists as a key?
- 'b' in data
- data.has_key('b')
- data.contains('b')
- data['b'] == True
- data.key('b')
Updating Values
Given my_dict = {'x': 1, 'y': 2}, which line will update the value of 'y' to 99?
- my_dict['y'] = 99
- update('y', 99)
- my_dict.update('y': 99)
- my_dict('y') = 99
- my_dict['y'] == 99
Successful Use Case
Which scenario best benefits from using a hash map (dictionary)?
- Looking up a user's email by their username
- Sorting a large list of numbers
- Iterating through numbers from 1 to 100,000
- Reversing a string
- Calculating the sum of a list
Collision Handling
What is the primary purpose of a hash function in a dictionary or hash map?
- To map keys to indices in the underlying array
- To sort keys alphabetically
- To convert all values to strings
- To encrypt the data stored
- To count the number of keys
Removing Elements
Given d = {'cat': 1, 'dog': 2}, which code removes the key 'dog'?
- d.pop('dog')
- delete d['dog']
- d.remove('dog')
- remove d['dog']
- pop(d, 'dog')
Typos and Similar Keys
What will happen if you try to access my_map['Adress'] when my_map = {'Address': '123 Main St', 'Email': 'john@example.com'}?
- A KeyError will be raised
- It will return the value for 'Address'
- It will return None
- It will automatically correct the typo
- It will raise a TypeError