0
0
Goprogramming~20 mins

Deleting map entries in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Go Map Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
}
Amap[a:1 b:2 c:3]
Bmap[]
Cmap[b:2]
Dmap[a:1 c:3]
Attempts:
2 left
💡 Hint
Deleting a key removes it from the map, so it won't appear in the output.
Predict Output
intermediate
2: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)
}
Amap[]
Bmap[3:]
Cmap[1:one 2:two]
Druntime error
Attempts:
2 left
💡 Hint
Deleting a key that does not exist does not cause an error.
🔧 Debug
advanced
2: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")
}
AThe map m is nil and cannot be used with delete.
BThe key "key" does not exist in the map.
CThe delete function requires a pointer to the map.
DThe map must be initialized with make before deleting.
Attempts:
2 left
💡 Hint
Check if the map is initialized before using delete.
Predict Output
advanced
2: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))
}
A2
B4
C0
D3
Attempts:
2 left
💡 Hint
Keys 2 and 4 are even and get deleted.
🧠 Conceptual
expert
2: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?
ADeleting keys during iteration is safe and the iteration will skip deleted keys.
BThe iteration order is undefined and deleting keys may cause some keys to be skipped or visited twice.
CDeleting keys during iteration causes a runtime panic.
DThe map is copied before iteration, so deleting keys does not affect the iteration.
Attempts:
2 left
💡 Hint
Map iteration order is random and deleting keys affects iteration unpredictably.