Challenge - 5 Problems
Hash Table Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Hash Function Modulo Operation
What is the output of the following Go code that simulates a simple hash function using modulo operation?
DSA Go
package main import "fmt" func main() { keys := []int{10, 22, 31, 4, 15} hashTableSize := 10 for _, key := range keys { hash := key % hashTableSize fmt.Printf("Key %d hashes to index %d\n", key, hash) } }
Attempts:
2 left
💡 Hint
Remember that modulo (%) gives the remainder after division.
✗ Incorrect
Each key is divided by the hash table size (10), and the remainder is the hash index. For example, 10 % 10 = 0, 22 % 10 = 2, etc.
🧠 Conceptual
intermediateUnderstanding Hash Collisions
Which of the following statements best describes a hash collision in a hash table?
Attempts:
2 left
💡 Hint
Think about what happens when two keys map to the same place.
✗ Incorrect
A hash collision happens when two different keys get the same hash index, causing them to compete for the same slot.
🔧 Debug
advancedIdentify the Error in Hash Function Implementation
What error will occur when running this Go code that tries to compute a hash index for a string key?
DSA Go
package main import ( "fmt" ) func hash(key string, size int) int { hashValue := 0 for i := 0; i < len(key); i++ { hashValue += int(key[i]) } return hashValue % size } func main() { h := hash("abc", 5) fmt.Println("Hash index:", h) }
Attempts:
2 left
💡 Hint
Sum the ASCII values of 'a', 'b', and 'c' and then modulo by 5.
✗ Incorrect
ASCII values: 'a' = 97, 'b' = 98, 'c' = 99. Sum = 294. 294 % 5 = 4. So output is 'Hash index: 4'.
🚀 Application
advancedChoosing a Hash Table Size
Why is it recommended to choose a prime number as the size of a hash table when using modulo-based hash functions?
Attempts:
2 left
💡 Hint
Think about how modulo with prime numbers affects key distribution.
✗ Incorrect
Using a prime number as the table size helps spread keys more uniformly, reducing clustering and collisions.
❓ Predict Output
expertOutput of Custom Hash Function with Collision Handling
What is the printed state of the hash table after inserting keys with linear probing collision handling?
DSA Go
package main import "fmt" func main() { hashTable := make([]int, 7) for i := range hashTable { hashTable[i] = -1 } keys := []int{10, 20, 15, 7} hash := func(key int) int { return key % len(hashTable) } for _, key := range keys { index := hash(key) for hashTable[index] != -1 { index = (index + 1) % len(hashTable) } hashTable[index] = key } for i, val := range hashTable { fmt.Printf("Index %d: %d\n", i, val) } }
Attempts:
2 left
💡 Hint
Check how linear probing moves to next index when collision occurs.
✗ Incorrect
Keys are inserted using linear probing (no collisions occur here):
- 10 % 7 = 3 → placed at 3
- 20 % 7 = 6 → placed at 6
- 15 % 7 = 1 → placed at 1
- 7 % 7 = 0 → placed at 0
Final state: Index 0: 7, 1: 15, 2: -1, 3: 10, 4: -1, 5: -1, 6: 20. Matches option C.
