A hash table is a data structure that stores data in key-value pairs. It uses a hash function to convert keys into an index where values are stored, allowing fast data retrieval.
Click to reveal answer
beginner
What does a hash function do in a hash table?
A hash function takes a key and returns an integer index. This index tells where to store or find the value in the hash table.
Click to reveal answer
beginner
Why do hash tables use arrays internally?
Hash tables use arrays because arrays allow fast access by index. The hash function gives the index, so the value can be quickly found or stored.
Click to reveal answer
intermediate
What is a collision in a hash table?
A collision happens when two different keys produce the same index from the hash function. The hash table must handle collisions to store both values correctly.
Click to reveal answer
intermediate
Name one common method to handle collisions in hash tables.
One common method is chaining, where each array index holds a list of key-value pairs. If multiple keys map to the same index, they are stored in the list.
Click to reveal answer
What does a hash function return?
AAn index to store or find data
BThe original key
CThe value stored
DA random number
✗ Incorrect
A hash function returns an index used to store or find data in the hash table.
What is stored in a hash table?
AKey-value pairs
BOnly values
COnly keys
DIndexes only
✗ Incorrect
Hash tables store key-value pairs for fast lookup.
What problem occurs when two keys map to the same index?
AOverflow
BUnderflow
CCollision
DSegmentation fault
✗ Incorrect
Collision happens when two keys map to the same index.
Which data structure is commonly used inside a hash table to handle collisions?
AStack
BQueue
CBinary tree
DLinked list
✗ Incorrect
Linked lists are commonly used to store multiple items at the same index (chaining).
Why are hash tables fast for searching?
AThey sort data
BThey use indexes from hash functions
CThey store data sequentially
DThey use recursion
✗ Incorrect
Hash tables use indexes from hash functions to quickly find data.
Explain how a hash table stores and retrieves data using a hash function.
Think about how you find a book in a library using a code.
You got /4 concepts.
Describe what a collision is in a hash table and one way to handle it.
Imagine two friends having the same locker number.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a hash function in a hash table?
easy
A. To convert a key into an index for storing data
B. To sort the keys in ascending order
C. To encrypt the data stored in the table
D. To count the number of elements in the table
Solution
Step 1: Understand the role of a hash function
A hash function takes a key and converts it into a number that can be used as an index in the hash table.
Step 2: Compare with other options
Sorting keys, encrypting data, or counting elements are not the main roles of a hash function in a hash table.
Final Answer:
To convert a key into an index for storing data -> Option A
Quick Check:
Hash function = key to index [OK]
Hint: Hash function maps keys to indexes quickly [OK]
Common Mistakes:
Thinking hash function sorts keys
Confusing hash function with encryption
Assuming hash function counts elements
2. Which of the following is the correct way to declare and initialize a map[string]int in Go?
easy
A. var m map[string]int
B. m := make(map[string]int)
C. var m = map[int]string{}
D. map m = make(map[string]int)
Solution
Step 1: Review Go syntax for map creation
In Go, the idiomatic way to create a map is using the short declaration with make, like m := make(map[string]int).
Step 2: Check other options for correctness
A declares a nil map without initialization. B uses incorrect types (map[int]string). D is invalid syntax (missing var or :=).
Final Answer:
m := make(map[string]int) -> Option B
Quick Check:
Use make with := for maps in Go [OK]
Hint: Use m := make(map[keyType]valueType) in Go [OK]
Common Mistakes:
Using var without make correctly
Incorrect map literal syntax
Wrong variable declaration order
3. What will be the output of the following Go code?
package main
import "fmt"
func main() {
m := make(map[string]int)
m["apple"] = 5
m["banana"] = 3
fmt.Println(m["apple"] + m["banana"])
}
medium
A. 0
B. 53
C. 8
D. Compilation error
Solution
Step 1: Understand map assignments
The map m stores integer values for keys "apple" and "banana" as 5 and 3 respectively.
Step 2: Calculate the sum printed
m["apple"] + m["banana"] equals 5 + 3 which is 8.
Final Answer:
8 -> Option C
Quick Check:
5 + 3 = 8 [OK]
Hint: Sum values by accessing keys directly [OK]
Common Mistakes:
Concatenating numbers as strings
Assuming default zero for missing keys
Expecting syntax errors
4. Identify the error in the following Go code that uses a hash map:
package main
import "fmt"
func main() {
var m map[string]int
m["key"] = 10
fmt.Println(m["key"])
}
medium
A. Missing import for map package
B. Key "key" is invalid syntax
C. fmt.Println cannot print map values
D. Map m is declared but not initialized before assignment
Solution
Step 1: Check map declaration and initialization
The map m is declared but not initialized with make, so it is nil and cannot be assigned to.
Step 2: Understand runtime error cause
Assigning m["key"] = 10 on a nil map causes a runtime panic.
Final Answer:
Map m is declared but not initialized before assignment -> Option D
Quick Check:
Maps must be initialized with make before use [OK]
Hint: Always initialize maps with make before adding keys [OK]
Common Mistakes:
Forgetting to initialize map
Thinking map keys need special syntax
Assuming fmt.Println can't print map values
5. You want to store user IDs (integers) mapped to usernames (strings) in Go. Which hash function property is most important to minimize collisions when keys are integers?
hard
A. The hash function should distribute keys uniformly across the table
B. The hash function should always return zero for simplicity
C. The hash function should sort keys before hashing
D. The hash function should convert integers to strings
Solution
Step 1: Understand collision in hash tables
Collisions happen when different keys map to the same index. Minimizing collisions improves performance.
Step 2: Identify the best hash function property
A good hash function distributes keys uniformly to reduce collisions. Returning zero or sorting keys does not help. Converting integers to strings is unnecessary.
Final Answer:
The hash function should distribute keys uniformly across the table -> Option A
Quick Check:
Uniform distribution reduces collisions [OK]
Hint: Uniform key spread reduces collisions in hash tables [OK]
Common Mistakes:
Choosing constant hash values
Sorting keys before hashing
Unnecessary type conversions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.