Bird
Raised Fist0
Goprogramming~20 mins

Map use cases in Go - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the primary use of a map in Go?
easy
A. To store data with keys for fast lookup
B. To perform mathematical calculations
C. To create user interfaces
D. To handle file input/output

Solution

  1. Step 1: Understand the purpose of a map

    A map in Go is a collection of key-value pairs, designed for fast data retrieval using keys.
  2. Step 2: Compare with other options

    Options B, C, and D describe unrelated tasks like calculations, UI, or file handling, which are not the main use of maps.
  3. Final Answer:

    To store data with keys for fast lookup -> Option A
  4. Quick Check:

    Maps = key-value storage [OK]
Hint: Maps store key-value pairs for quick access [OK]
Common Mistakes:
  • Confusing maps with slices or arrays
  • Thinking maps are for calculations
  • Assuming maps handle UI or files
2. Which of the following is the correct way to create an empty map in Go?
easy
A. m := make([]string, 0)
B. var m map[string]int = make(map[string]int)
C. m := map[string]int
D. m := []int{}

Solution

  1. Step 1: Identify map creation syntax

    In Go, maps are created using make(map[keyType]valueType). var m map[string]int = make(map[string]int) uses this correctly.
  2. Step 2: Check other options

    m := []int{} creates a slice, not a map. m := map[string]int is invalid syntax for map creation. m := make([]string, 0) creates a slice, not a map.
  3. Final Answer:

    var m map[string]int = make(map[string]int) -> Option B
  4. Quick Check:

    Use make() to create maps [OK]
Hint: Use make() to create maps before use [OK]
Common Mistakes:
  • Using slice syntax instead of map syntax
  • Not using make() to initialize maps
  • Confusing map and slice creation
3. What will be the output of this Go code?
m := make(map[string]int)
m["apple"] = 5
m["banana"] = 3
fmt.Println(m["apple"] + m["banana"])
medium
A. 8
B. 53
C. 0
D. Compilation error

Solution

  1. Step 1: Understand map assignments

    The map m stores integers with string keys. "apple" maps to 5, "banana" maps to 3.
  2. Step 2: Calculate the sum

    Adding m["apple"] + m["banana"] equals 5 + 3 = 8.
  3. Final Answer:

    8 -> Option A
  4. Quick Check:

    5 + 3 = 8 [OK]
Hint: Sum values by accessing keys directly [OK]
Common Mistakes:
  • Concatenating values as strings
  • Expecting zero for existing keys
  • Thinking code causes error
4. What is wrong with this Go code snippet?
var m map[string]int
m["key"] = 10
fmt.Println(m["key"])
medium
A. Cannot assign int values to map
B. Key "key" is invalid syntax
C. Map is not initialized with make() before use
D. fmt.Println cannot print map values

Solution

  1. Step 1: Check map initialization

    The map m is declared but not initialized with make(). This causes a runtime panic on assignment.
  2. Step 2: Understand consequences

    Without initialization, m["key"] = 10 causes a panic: assignment to nil map.
  3. Final Answer:

    Map is not initialized with make() before use -> Option C
  4. Quick Check:

    Initialize maps with make() [OK]
Hint: Always initialize maps with make() before adding keys [OK]
Common Mistakes:
  • Forgetting to initialize map before use
  • Assuming declaration is enough
  • Misunderstanding map assignment rules
5. Given a map m := map[string]int{"a": 1, "b": 0, "c": 3}, which code snippet correctly checks if key "b" exists and its value is non-zero?
hard
A. if _, ok := m["b"]; ok { fmt.Println("Exists and non-zero") }
B. if m["b"] == 0 { fmt.Println("Exists and non-zero") }
C. if m["b"] >= 0 { fmt.Println("Exists and non-zero") }
D. if val, ok := m["b"]; ok && val != 0 { fmt.Println("Exists and non-zero") }

Solution

  1. Step 1: Understand map key existence check

    Using val, ok := m["b"] lets us check if key "b" exists (ok) and get its value (val).
  2. Step 2: Check value is non-zero

    The condition ok && val != 0 ensures key exists and value is not zero.
  3. Final Answer:

    if val, ok := m["b"]; ok && val != 0 { fmt.Println("Exists and non-zero") } -> Option D
  4. Quick Check:

    Check key existence and value together [OK]
Hint: Use val, ok := map[key] to check existence and value [OK]
Common Mistakes:
  • Checking value without verifying key existence
  • Assuming zero means key missing
  • Using only ok without value check
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard