Challenge - 5 Problems
Map Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:00remaining
Purpose of maps in Go
Why do programmers use maps in Go?
Attempts:
2 left
💡 Hint
Think about how you find a phone number quickly in a phone book.
✗ Incorrect
Maps store data as key-value pairs, allowing quick access to values using keys, similar to looking up a name in a dictionary.
❓ Predict Output
intermediate1:30remaining
Output of map lookup
What is the output of this Go code?
Go
package main import "fmt" func main() { m := map[string]int{"apple": 5, "banana": 3} fmt.Println(m["apple"]) }
Attempts:
2 left
💡 Hint
Look at the value stored for the key "apple".
✗ Incorrect
The map stores 5 for the key "apple", so printing m["apple"] outputs 5.
❓ Predict Output
advanced1:30remaining
Map key existence check
What does this Go code print?
Go
package main import "fmt" func main() { m := map[string]int{"x": 10} val, ok := m["y"] fmt.Println(val, ok) }
Attempts:
2 left
💡 Hint
Check if the key "y" exists in the map.
✗ Incorrect
The key "y" is not in the map, so val is zero value 0 and ok is false.
🔧 Debug
advanced2:00remaining
Why does this map assignment cause a panic?
What causes the panic in this Go code?
Go
package main func main() { var m map[string]int m["key"] = 1 }
Attempts:
2 left
💡 Hint
Maps must be created before use.
✗ Incorrect
Declaring a map variable without initializing it results in a nil map. Assigning to a nil map causes a runtime panic.
🧠 Conceptual
expert2:00remaining
Why maps are preferred over slices for lookups
Why is a map preferred over a slice when you need to find an item by a key quickly?
Attempts:
2 left
💡 Hint
Think about how fast you can find a word in a dictionary versus a list.
✗ Incorrect
Maps use hashing to find keys quickly in constant time, while slices require checking each element until the key is found.