0
0
Goprogramming~20 mins

Why maps are used in Go - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Map Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:00remaining
Purpose of maps in Go
Why do programmers use maps in Go?
ATo create graphical user interfaces
BTo perform mathematical calculations efficiently
CTo store key-value pairs for fast data lookup
DTo manage concurrent processes automatically
Attempts:
2 left
💡 Hint
Think about how you find a phone number quickly in a phone book.
Predict Output
intermediate
1:30remaining
Output of map lookup
What is the output of this Go code?
Go
package main
import "fmt"
func main() {
  m := map[string]int{"apple": 5, "banana": 3}
  fmt.Println(m["apple"])
}
Abanana
B0
Capple
D5
Attempts:
2 left
💡 Hint
Look at the value stored for the key "apple".
Predict Output
advanced
1:30remaining
Map key existence check
What does this Go code print?
Go
package main
import "fmt"
func main() {
  m := map[string]int{"x": 10}
  val, ok := m["y"]
  fmt.Println(val, ok)
}
A0 true
B0 false
C10 false
D10 true
Attempts:
2 left
💡 Hint
Check if the key "y" exists in the map.
🔧 Debug
advanced
2:00remaining
Why does this map assignment cause a panic?
What causes the panic in this Go code?
Go
package main
func main() {
  var m map[string]int
  m["key"] = 1
}
AThe map is nil and not initialized before assignment
BThe key "key" is invalid for map assignment
CThe map type is incorrect for string keys
DThe map must be declared with make inside main
Attempts:
2 left
💡 Hint
Maps must be created before use.
🧠 Conceptual
expert
2:00remaining
Why maps are preferred over slices for lookups
Why is a map preferred over a slice when you need to find an item by a key quickly?
AMaps provide constant time lookup, slices require scanning all elements
BSlices use less memory than maps for large data
CMaps keep elements in order, slices do not
DSlices automatically sort elements, maps do not
Attempts:
2 left
💡 Hint
Think about how fast you can find a word in a dictionary versus a list.