Bird
Raised Fist0
Goprogramming~20 mins

Map creation in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Go Map Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Go map creation code?
Consider the following Go code snippet that creates and initializes a map. What will be printed when this program runs?
Go
package main
import "fmt"
func main() {
    m := map[string]int{"apple": 5, "banana": 3}
    fmt.Println(m["apple"] + m["banana"])
}
ACompilation error
B8
C0
D53
Attempts:
2 left
💡 Hint
Remember that map values can be accessed by their keys and you can add integers.
❓ Predict Output
intermediate
2:00remaining
What is the length of the map after this code runs?
Look at this Go code that creates a map and adds elements. What is the length of the map after execution?
Go
package main
import "fmt"
func main() {
    m := make(map[int]string)
    m[1] = "one"
    m[2] = "two"
    m[1] = "uno"
    fmt.Println(len(m))
}
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
Maps cannot have duplicate keys; adding a key again updates the value.
❓ Predict Output
advanced
2:00remaining
What error does this Go code produce?
This Go code tries to create a map with a nil key. What error will it produce when compiled or run?
Go
package main
func main() {
    m := make(map[*int]string)
    var p *int = nil
    m[p] = "nil pointer"
}
Apanic: runtime error: hash of nil pointer
BCompilation error: invalid map key type
Cpanic: assignment to entry in nil map
DNo error, map accepts nil pointer as key
Attempts:
2 left
💡 Hint
In Go, nil pointers can be used as map keys if the key type is a pointer.
❓ Predict Output
advanced
2:00remaining
What is the output of this Go map creation with composite literals?
What will this Go program print when run?
Go
package main
import "fmt"
func main() {
    m := map[string][]int{
        "a": {1, 2},
        "b": {3, 4},
    }
    m["a"] = append(m["a"], 5)
    fmt.Println(m["a"])
}
A[1 2 5]
B[1 2]
C[5]
DCompilation error: cannot use composite literal
Attempts:
2 left
💡 Hint
Appending to a slice inside a map updates the slice stored at that key.
🧠 Conceptual
expert
3:00remaining
Which option correctly creates a map with keys of type struct and values of type int?
You want to create a Go map where keys are structs with two int fields and values are integers. Which option correctly declares and initializes this map with one entry where the struct has fields 1 and 2, and the value is 10?
Am := map[struct{x, y int}]int{{1, 2}: 10}
Bm := map[struct{x int; y int}]int{struct{x int; y int}{1, 2}: 10}
Cm := map[struct{x int; y int}]int{{x: 1, y: 2}: 10}
D}01 :}2 :y ,1 :x{{tni]}tni y ;tni x{tcurts[pam =: m
Attempts:
2 left
💡 Hint
Struct field declarations in Go use semicolons or newlines, not commas, and composite literals can use field names.

Practice

(1/5)
1. What is the correct way to create an empty map with string keys and int values in Go?
easy
A. m := make(map[string]int)
B. m := map[string]int()
C. m := new(map[string]int)
D. m := []string{}

Solution

  1. Step 1: Understand map creation syntax

    In Go, the built-in function make is used to create maps with specified key and value types.
  2. Step 2: Identify the correct syntax for an empty map

    make(map[string]int) creates an empty map with string keys and int values.
  3. Final Answer:

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

    Use make(map[keyType]valueType) to create maps [OK]
Hint: Use make() with map[keyType]valueType to create maps [OK]
Common Mistakes:
  • Using new() instead of make() for maps
  • Trying to create map with slice syntax
  • Confusing map literal with empty slice
2. Which of the following is a syntax error when creating a map in Go?
easy
A. m := make(map[int]string)
B. m := make(map[float64]int)
C. m := make(map[[]int]string)
D. m := map[int]string{}

Solution

  1. Step 1: Check key type validity

    Map keys must be comparable types. Slices ([]int) are not comparable in Go.
  2. Step 2: Identify invalid map key type

    Using []int as a key type causes a syntax error because slices cannot be map keys.
  3. Final Answer:

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

    Map keys must be comparable types [OK]
Hint: Map keys cannot be slices or maps, only comparable types [OK]
Common Mistakes:
  • Using slices or maps as map keys
  • Confusing map literal with slice literal
  • Ignoring key type restrictions
3. What will be the output of the following Go code?
m := make(map[string]int)
m["apple"] = 5
m["banana"] = 3
fmt.Println(m["apple"] + m["banana"])
medium
A. 53
B. 8
C. 0
D. Compilation error

Solution

  1. Step 1: Understand map assignments

    The map m stores key-value pairs: "apple"=5 and "banana"=3.
  2. Step 2: Calculate the sum of values

    Adding m["apple"] (5) and m["banana"] (3) results in 8.
  3. Final Answer:

    8 -> Option B
  4. Quick Check:

    5 + 3 = 8 [OK]
Hint: Sum map values by accessing keys directly [OK]
Common Mistakes:
  • Concatenating values as strings instead of adding
  • Expecting zero for existing keys
  • Forgetting to initialize map before use
4. Identify the error in the following Go code snippet:
var m map[string]int
m["key"] = 10
fmt.Println(m["key"])
medium
A. No error, prints 10
B. Syntax error in map declaration
C. Key type must be int, not string
D. Map declared but not initialized, causes runtime panic

Solution

  1. Step 1: Check map initialization

    The map m is declared but not initialized with make, so it is nil.
  2. Step 2: Understand runtime behavior

    Assigning to a nil map causes a runtime panic (error) because the map is not ready to store values.
  3. Final Answer:

    Map declared but not initialized, causes runtime panic -> Option D
  4. Quick Check:

    Declare + make before use to avoid panic [OK]
Hint: Always initialize maps with make() before adding keys [OK]
Common Mistakes:
  • Forgetting to initialize map before assignment
  • Assuming declaration alone creates usable map
  • Confusing map declaration with slice declaration
5. Given a slice of strings keys := []string{"a", "b", "c"} and a slice of ints values := []int{1, 2, 3}, which code snippet correctly creates a map from keys to values?
hard
A. m := make(map[string]int) for i := range keys { m[keys[i]] = values[i] }
B. m := map[string]int{} for i, v := range values { m[v] = keys[i] }
C. m := make(map[int]string) for i := 0; i < len(keys); i++ { m[i] = keys[i] }
D. m := make(map[string]int) for i := 0; i < len(values); i++ { m[values[i]] = keys[i] }

Solution

  1. Step 1: Match keys and values correctly

    Keys are strings, values are ints. The map must have type map[string]int.
  2. Step 2: Verify loop and assignment

    m := make(map[string]int) for i := range keys { m[keys[i]] = values[i] } loops over keys and assigns m[keys[i]] = values[i], correctly pairing keys to values.
  3. Final Answer:

    m := make(map[string]int) for i := range keys { m[keys[i]] = values[i] } -> Option A
  4. Quick Check:

    Loop keys, assign values to map[key] [OK]
Hint: Loop keys, assign values to map[key] for correct pairing [OK]
Common Mistakes:
  • Swapping keys and values in map assignment
  • Using wrong map key or value types
  • Not initializing map before assignment
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