Maps / Dictionaries Flashcards
(49 cards)
What is a map in data structures?
A collection that stores key-value pairs.
What is a dictionary in Python?
A built-in data structure that functions as a map.
What is the primary purpose of a map?
To associate a unique key with a value.
Can map keys be duplicated?
No, all keys in a map must be unique.
Can map values be duplicated?
Yes, values can be repeated.
What is the time complexity of inserting into a hash map?
O(1) average case.
What is the time complexity of searching in a hash map?
O(1) average case.
What is the time complexity of deleting from a hash map?
O(1) average case.
What is a hash map?
A map implemented using a hash table.
What is a tree map?
A map implemented using a balanced binary search tree.
What is the time complexity of operations in a tree map?
O(log n).
What is the role of a hash function in a map?
To convert a key into an index for storage.
What happens when two keys hash to the same index?
A collision occurs, which must be resolved.
What are the common ways to resolve hash collisions?
Chaining and open addressing.
How do you access a value by key in a map?
Use the key as an index (e.g., map[key]).
What happens if you access a nonexistent key in a map?
It raises an error or returns null/None, depending on the language.
How do you safely access a value in Python dictionaries?
Use the get() method.
How do you insert or update a value in a map?
Assign a value to the key (e.g., map[key] = value).
What is the space complexity of a map?
O(n), where n is the number of key-value pairs.
What is an ordered map?
A map that maintains insertion order (e.g., Python 3.7+, LinkedHashMap in Java).
What is a default dictionary?
A dictionary that returns a default value when a key is missing.
What is a nested dictionary?
A dictionary where values are also dictionaries.
How do you iterate through a map?
Use a for-loop over keys, values, or key-value pairs.
How do you delete a key-value pair from a map?
Use the del statement or remove method.