Why are maps used for?
For example, here is an inefficient program that uses a loop to find an element among millions of elements.
millions := []int{/* millions of elements */}
for _, v := range millions {
if v == userQuery {
// do something
}
}
- Maps allow fast-lookup for map keys in O(1) time CORRECT
- Maps allow fast-lookup for map keys in O(n) time
- Maps allow fast-traversal on map keys in O(1) time
1: That's right. Maps work in O(1) in average for fast-lookup.
2: Map doesn't work in O(n) time for key lookup.
When should you not use a map?
- To find an element through a key
- To loop over the map keys CORRECT
- To add structured data to your program
1: That is when you use a map.
2: Right! Looping over map keys happen in O(n) time. So, maps are the worst data structures for key traversing.
3: Maps don't allow you to add structured data to your program.
Which type below cannot be a map key?
- map[string]int
- []string
- []int
- []bool
- All of them CORRECT
5: Slices, maps, and function values are not comparable. So, they cannot be map keys.
Which are the key and element types of the map below?
map[string]map[int]bool
- Key: string Element: bool
- Key: string Element: int
- Key: string Element: map[int]
- Key: string Element: map[int]bool CORRECT
4: The map contains other maps. The element type of a map can be of any type.
What is a map value behind the scenes?
- A map header
- A pointer to a map header CORRECT
- Tiny data structure with 3 fields: Pointer, Length and Capacity
2: That's right. Maps are complex data structures. However, each map value is only a pointer to a map header (which is a more complex data structure).