Maps / Dictionaries Flashcards

(49 cards)

1
Q

What is a map in data structures?

A

A collection that stores key-value pairs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a dictionary in Python?

A

A built-in data structure that functions as a map.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the primary purpose of a map?

A

To associate a unique key with a value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Can map keys be duplicated?

A

No, all keys in a map must be unique.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can map values be duplicated?

A

Yes, values can be repeated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the time complexity of inserting into a hash map?

A

O(1) average case.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the time complexity of searching in a hash map?

A

O(1) average case.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the time complexity of deleting from a hash map?

A

O(1) average case.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a hash map?

A

A map implemented using a hash table.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a tree map?

A

A map implemented using a balanced binary search tree.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the time complexity of operations in a tree map?

A

O(log n).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the role of a hash function in a map?

A

To convert a key into an index for storage.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What happens when two keys hash to the same index?

A

A collision occurs, which must be resolved.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the common ways to resolve hash collisions?

A

Chaining and open addressing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you access a value by key in a map?

A

Use the key as an index (e.g., map[key]).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What happens if you access a nonexistent key in a map?

A

It raises an error or returns null/None, depending on the language.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How do you safely access a value in Python dictionaries?

A

Use the get() method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How do you insert or update a value in a map?

A

Assign a value to the key (e.g., map[key] = value).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the space complexity of a map?

A

O(n), where n is the number of key-value pairs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is an ordered map?

A

A map that maintains insertion order (e.g., Python 3.7+, LinkedHashMap in Java).

21
Q

What is a default dictionary?

A

A dictionary that returns a default value when a key is missing.

22
Q

What is a nested dictionary?

A

A dictionary where values are also dictionaries.

23
Q

How do you iterate through a map?

A

Use a for-loop over keys, values, or key-value pairs.

24
Q

How do you delete a key-value pair from a map?

A

Use the del statement or remove method.

25
What is the keys() method in a dictionary?
Returns a view of all keys.
26
What is the values() method in a dictionary?
Returns a view of all values.
27
What is the items() method in a dictionary?
Returns a view of all key-value pairs.
28
What happens if you insert a key that already exists?
Its value is updated.
29
Can keys in a map be mutable?
No, keys must be hashable and immutable.
30
What is the difference between a list and a dictionary?
Lists use integer indices; dictionaries use keys.
31
What is a common use case for maps?
Counting frequency of elements.
32
What is a multi-map?
A map that allows multiple values for a single key.
33
How do you simulate a multi-map in Python?
Use a dictionary with lists or sets as values.
34
What is the difference between map and set?
Maps store key-value pairs; sets store only keys.
35
Can a map have null or None as a key?
Yes, depending on the language.
36
What is the load factor in a hash map?
The ratio of the number of elements to the number of buckets.
37
What happens when the load factor is too high?
The map may resize or performance may degrade.
38
What is rehashing in a map?
Resizing the map and re-inserting all elements.
39
How is a map implemented in C++?
Using std::map (tree) and std::unordered_map (hash).
40
How is a map implemented in Java?
Using HashMap, TreeMap, or LinkedHashMap.
41
How do you check if a key exists in a map?
Use the in keyword or containsKey method.
42
How do you get the number of elements in a map?
Use the len() function or size() method.
43
What is the time complexity of checking key existence in a hash map?
O(1) on average.
44
What is a symbol table?
Another name for a map, often used in compilers.
45
What is a sparse map?
A map used to store keys with widely spaced or rare values.
46
What is a frequency map?
A map where keys are elements and values are their counts.
47
What is the key difference between HashMap and TreeMap in Java?
HashMap is unordered and faster; TreeMap is sorted.
48
Can a map be used to sort data?
Yes, if the map maintains order, like a TreeMap.
49
Can values in a map be complex objects?
Yes, values can be any data type including lists or other maps.