Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the hash table with empty chains.
DSA Go
hashTable := make([][]int, [1])Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 creates no chains, so no place to store data.
Using 1 creates only one chain, which defeats chaining benefits.
✗ Incorrect
The hash table size is set to 5 to create 5 chains (buckets) for collision handling.
2fill in blank
mediumComplete the code to compute the hash index for a given key.
DSA Go
index := key [1] len(hashTable)
Attempts:
3 left
💡 Hint
Common Mistakes
Using * or + changes the index unpredictably.
Using / gives a float, which is invalid for indexing.
✗ Incorrect
The modulo operator (%) ensures the index fits within the hash table size.
3fill in blank
hardFix the error in the code to append a key to the correct chain.
DSA Go
hashTable[index] = append(hashTable[[1]], key)Attempts:
3 left
💡 Hint
Common Mistakes
Using the key as index causes runtime errors.
Using the whole hashTable as index is invalid.
✗ Incorrect
We append the key to the chain at position 'index' in the hash table.
4fill in blank
hardFill both blanks to check if a key exists in the chain.
DSA Go
for _, v := range hashTable[[1]] { if v [2] key { return true } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' reverses the logic and never finds the key.
Using 'key' as index causes errors.
✗ Incorrect
We loop over the chain at 'index' and check if any value equals the key.
5fill in blank
hardFill all three blanks to remove a key from the chain correctly.
DSA Go
chain := hashTable[[1]] for i, v := range chain { if v [2] key { hashTable[[3]] = append(chain[:i], chain[i+1:]...) break } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' causes the wrong element to be removed.
Using wrong index in assignment breaks the chain.
✗ Incorrect
We find the key in the chain at 'index' and remove it by slicing out the element.
