Bird
Raised Fist0
Goprogramming~20 mins

Adding and updating values in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Go Map Mastery
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 code when adding and updating map values?
Consider the following Go program that adds and updates values in a map. What will it print?
Go
package main
import "fmt"
func main() {
    scores := map[string]int{"Alice": 10, "Bob": 15}
    scores["Alice"] += 5
    scores["Charlie"] = 20
    fmt.Println(scores)
}
Amap[Alice:15 Bob:15 Charlie:20]
Bmap[Alice:10 Bob:15 Charlie:20]
Cmap[Alice:15 Bob:15]
Dmap[Alice:5 Bob:15 Charlie:20]
Attempts:
2 left
💡 Hint
Remember that updating a map value with += adds to the existing value.
❓ Predict Output
intermediate
2:00remaining
What happens when you update a map value for a non-existent key?
Look at this Go code snippet. What will be the output?
Go
package main
import "fmt"
func main() {
    ages := map[string]int{"John": 30}
    ages["Mary"] += 5
    fmt.Println(ages)
}
Amap[John:30 Mary:5]
Bmap[John:30 Mary:0]
Cmap[John:30]
DCompilation error
Attempts:
2 left
💡 Hint
When you add to a zero value in a map, Go initializes it to zero first.
🔧 Debug
advanced
2:00remaining
Why does this Go code cause a runtime panic when updating a map?
Examine the code below. It causes a runtime panic. What is the reason?
Go
package main
func main() {
    var data map[string]int
    data["key"] = 1
}
AThe map must be declared with 'make' keyword to allow updates.
BThe key 'key' is invalid for map assignment.
CThe map 'data' is nil and not initialized before assignment.
DThe map keys must be integers, not strings.
Attempts:
2 left
💡 Hint
Maps must be initialized before you can add or update values.
❓ Predict Output
advanced
2:00remaining
What is the output after updating nested map values?
Given this Go code with nested maps, what will be printed?
Go
package main
import "fmt"
func main() {
    users := map[string]map[string]int{
        "user1": {"score": 10},
        "user2": {"score": 20},
    }
    users["user1"]["score"] += 5
    users["user3"] = map[string]int{"score": 15}
    fmt.Println(users)
}
ACompilation error due to nested map assignment
Bmap[user1:map[score:15] user2:map[score:20] user3:map[score:15]]
Cmap[user1:map[score:15] user2:map[score:20]]
Dmap[user1:map[score:10] user2:map[score:20] user3:map[score:15]]
Attempts:
2 left
💡 Hint
You can update nested map values by accessing keys step by step.
🧠 Conceptual
expert
2:00remaining
How many items are in the map after these operations?
Consider this Go code snippet. How many key-value pairs does the map contain at the end?
Go
package main
import "fmt"
func main() {
    m := make(map[int]string)
    for i := 0; i < 5; i++ {
        m[i] = fmt.Sprintf("val%d", i)
    }
    m[2] = "updated"
    delete(m, 4)
    fmt.Println(len(m))
}
A6
B5
C3
D4
Attempts:
2 left
💡 Hint
Remember that deleting a key reduces the map size by one.

Practice

(1/5)
1. What happens when you assign a value to a new key in a Go map using mapName[key] = value?
easy
A. A new key-value pair is added to the map.
B. The map is deleted.
C. The map becomes empty.
D. The program crashes.

Solution

  1. Step 1: Understand map assignment

    Assigning a value to a new key in a map adds that key with the value.
  2. Step 2: Differentiate from update

    If the key exists, the value updates; if not, a new pair is created.
  3. Final Answer:

    A new key-value pair is added to the map. -> Option A
  4. Quick Check:

    Adding new key = new pair added [OK]
Hint: Assigning to new key adds pair, not error or delete [OK]
Common Mistakes:
  • Thinking map clears on new key assignment
  • Confusing adding with deleting
  • Assuming program crashes on new key
2. Which of the following is the correct syntax to update the value for key "age" to 30 in a Go map named person?
easy
A. person["age"] := 30
B. person["age"] = 30
C. person.age = 30
D. person.update("age", 30)

Solution

  1. Step 1: Recall map update syntax

    In Go, map values are updated by assigning with = operator: map[key] = value.
  2. Step 2: Check each option

    person["age"] = 30 uses correct syntax. person["age"] := 30 uses := which is for variable declaration, not map update. person.age = 30 uses dot notation which is invalid for maps. person.update("age", 30) uses a method that does not exist.
  3. Final Answer:

    person["age"] = 30 -> Option B
  4. Quick Check:

    Use = to update map value [OK]
Hint: Use =, not := or dot, to update map values [OK]
Common Mistakes:
  • Using := instead of = for map update
  • Using dot notation for map keys
  • Trying to call update method on map
3. What is the output of this Go code?
m := map[string]int{"a": 1, "b": 2}
m["a"] = 5
fmt.Println(m["a"])
medium
A. 1
B. 2
C. 5
D. 0

Solution

  1. Step 1: Analyze initial map values

    The map m starts with keys "a":1 and "b":2.
  2. Step 2: Update value for key "a"

    m["a"] = 5 changes the value for "a" from 1 to 5.
  3. Step 3: Print updated value

    fmt.Println(m["a"]) prints the updated value 5.
  4. Final Answer:

    5 -> Option C
  5. Quick Check:

    Updated value prints 5 [OK]
Hint: Updated map key prints new value, not old [OK]
Common Mistakes:
  • Expecting old value instead of updated
  • Confusing key "b" value with "a"
  • Thinking print shows zero value
4. Identify the error in this Go code snippet that tries to add a new key-value pair to a map:
var m map[string]int
m["new"] = 10
medium
A. Key "new" is a reserved word and cannot be used.
B. Syntax error: missing semicolon after map declaration.
C. Value 10 must be a string, not int.
D. Map m is nil and not initialized before adding a key.

Solution

  1. Step 1: Understand map initialization

    Declaring var m map[string]int creates a nil map. It must be initialized with make() before adding keys.
  2. Step 2: Identify error cause

    Assigning m["new"] = 10 on a nil map causes runtime panic because map is not initialized.
  3. Final Answer:

    Map m is nil and not initialized before adding a key. -> Option D
  4. Quick Check:

    Maps must be initialized before adding keys [OK]
Hint: Always initialize maps with make() before adding keys [OK]
Common Mistakes:
  • Assuming var declares initialized map
  • Thinking key "new" is invalid
  • Confusing value types for map
5. Given a map inventory := map[string]int{"apple": 5, "banana": 3}, write the correct Go code to add 2 more apples to the inventory without overwriting the existing count.
hard
A. inventory["apple"] += 2
B. inventory["apple"] = inventory["apple"] - 2
C. inventory["apple"] = 2
D. inventory.add("apple", 2)

Solution

  1. Step 1: Understand current value

    Current count for "apple" is 5 in the map.
  2. Step 2: Add 2 without overwriting

    Use += operator to add 2 to existing value: inventory["apple"] += 2.
  3. Step 3: Check other options

    inventory["apple"] = 2 overwrites with 2, not add. inventory["apple"] = inventory["apple"] - 2 subtracts 2. inventory.add("apple", 2) uses a non-existent method.
  4. Final Answer:

    inventory["apple"] += 2 -> Option A
  5. Quick Check:

    Use += to add to map value [OK]
Hint: Use += to add to existing map value, not = [OK]
Common Mistakes:
  • Overwriting value instead of adding
  • Subtracting instead of adding
  • Using non-existent map methods
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