0
0
Goprogramming~20 mins

Map use cases in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Map Mastery in Go
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Map key existence check output
What is the output of this Go program that checks if a key exists in a map?
Go
package main
import "fmt"
func main() {
    m := map[string]int{"apple": 5, "banana": 3}
    val, ok := m["orange"]
    fmt.Println(val, ok)
}
A3 true
B0 true
C0 false
DCompilation error
Attempts:
2 left
💡 Hint
Check what happens when you access a map with a key that does not exist.
Predict Output
intermediate
2:00remaining
Map iteration order output
What is the output of this Go program that iterates over a map?
Go
package main
import "fmt"
func main() {
    m := map[int]string{1: "one", 2: "two", 3: "three"}
    for k, v := range m {
        fmt.Printf("%d:%s ", k, v)
    }
}
A1:one 2:two 3:three
BThe output order is random and may vary each run
C3:three 2:two 1:one
DCompilation error
Attempts:
2 left
💡 Hint
Remember how Go handles map iteration order.
🔧 Debug
advanced
2:00remaining
Identify the runtime error in map access
What runtime error will this Go program produce when run?
Go
package main
func main() {
    var m map[string]int
    m["key"] = 10
}
Apanic: index out of range
BCompilation error: map not initialized
CNo error, program runs fine
Dpanic: assignment to entry in nil map
Attempts:
2 left
💡 Hint
Think about what happens if you assign to a nil map.
🧠 Conceptual
advanced
2:00remaining
Map key type restrictions
Which of the following types can be used as keys in Go maps?
AStructs with only comparable fields
BArrays
CSlices
DMaps
Attempts:
2 left
💡 Hint
Map keys must be comparable types in Go.
🚀 Application
expert
3:00remaining
Count word frequency using a map
What is the final value of the map after running this Go program that counts word frequency?
Go
package main
import (
    "fmt"
    "strings"
)
func main() {
    text := "go go gophers go"
    words := strings.Fields(text)
    freq := make(map[string]int)
    for _, w := range words {
        freq[w]++
    }
    fmt.Println(freq)
}
Amap[go:3 gophers:1]
Bmap[go:4 gophers:1]
Cmap[go:3 gophers:0]
Dmap[go:2 gophers:1]
Attempts:
2 left
💡 Hint
Count how many times 'go' appears in the text.