Imagine you want to store and quickly find phone numbers by name. You can use a list or a hash map. Why is a hash map better for this?
Think about how long it takes to find a name in a phone book if you check every entry versus using an index.
A hash map uses a special function to jump directly to the place where the key is stored, making search very fast. A list requires checking each item until the key is found, which is slower.
Consider this Go code that inserts keys and values into a map. What will be printed?
package main import "fmt" func main() { m := make(map[string]int) m["apple"] = 5 m["banana"] = 3 m["orange"] = 7 fmt.Println(m["banana"]) }
Look at the key used inside fmt.Println and find its value in the map.
The code prints the value stored for the key "banana", which is 3.
What happens if you try to get a value from a Go map using a key that does not exist?
Consider this code snippet:
m := map[string]int{"x": 10}
value := m["y"]
fmt.Println(value)Think about what Go returns when a key is not found in a map of integers.
In Go, accessing a missing key in a map returns the zero value of the value type. For int, zero value is 0, so it prints 0 without error.
You have a list of 1 million user IDs and want to check if a given ID exists quickly. Which data structure helps most and why?
Think about how search time changes as data size grows for each structure.
A hash map uses a hash function to jump directly to the key location, making search very fast even for large data. Other structures require checking multiple elements.
Given this Go code, what is the printed map content?
package main import "fmt" func main() { m := map[string]int{"a": 1, "b": 2} m["c"] = 3 delete(m, "a") m["b"] = 4 fmt.Println(m) }
Track each operation step by step: insert, delete, update.
Initially map has a:1, b:2. Insert c:3 adds c:3. Delete a removes key "a". Update b to 4 changes b's value. Final map is b:4, c:3.
