Challenge - 5 Problems
Go Map Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output after deleting a key from a map?
Consider the following Go code that deletes a key from a map. What will be printed?
Go
package main import "fmt" func main() { m := map[string]int{"a": 1, "b": 2, "c": 3} delete(m, "b") fmt.Println(m) }
Attempts:
2 left
💡 Hint
Deleting a key removes it from the map, so it won't appear in the output.
✗ Incorrect
The delete function removes the key "b" from the map. So the map only contains keys "a" and "c" after deletion.
❓ Predict Output
intermediate2:00remaining
What happens when deleting a non-existent key?
What will be the output of this Go program?
Go
package main import "fmt" func main() { m := map[int]string{1: "one", 2: "two"} delete(m, 3) fmt.Println(m) }
Attempts:
2 left
💡 Hint
Deleting a key that does not exist does not cause an error.
✗ Incorrect
Deleting a non-existent key does nothing. The map remains unchanged.
🔧 Debug
advanced2:00remaining
Why does this code cause a runtime panic?
This Go code tries to delete a key from a map but causes a runtime panic. What is the cause?
Go
package main func main() { var m map[string]int delete(m, "key") }
Attempts:
2 left
💡 Hint
Check if the map is initialized before using delete.
✗ Incorrect
A nil map cannot be used with delete; it causes a runtime panic.
❓ Predict Output
advanced2:00remaining
How many entries remain after deleting keys in a loop?
What is the length of the map after deleting keys in this Go code?
Go
package main import "fmt" func main() { m := map[int]int{1:10, 2:20, 3:30, 4:40} for k := range m { if k%2 == 0 { delete(m, k) } } fmt.Println(len(m)) }
Attempts:
2 left
💡 Hint
Keys 2 and 4 are even and get deleted.
✗ Incorrect
Keys 2 and 4 are deleted, leaving keys 1 and 3, so length is 2.
🧠 Conceptual
expert2:00remaining
What is the behavior of deleting keys during map iteration?
In Go, what is the guaranteed behavior when deleting keys from a map while iterating over it?
Attempts:
2 left
💡 Hint
Map iteration order is random and deleting keys affects iteration unpredictably.
✗ Incorrect
Go's map iteration order is random and deleting keys during iteration can cause keys to be skipped or visited multiple times. This behavior is not guaranteed to be consistent.