Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a hash table using C# Dictionary.
DSA C#
Dictionary<int, string> hashTable = new Dictionary<int, [1]>();Attempts:
3 left
💡 Hint
Common Mistakes
Using int as value type instead of string.
Using bool or double which are unrelated to the stored data.
✗ Incorrect
The Dictionary key is int and the value type is string, so we use Dictionary.
2fill in blank
mediumComplete the code to add a key-value pair to the hash table.
DSA C#
hashTable.[1](5, "apple");
Attempts:
3 left
💡 Hint
Common Mistakes
Using Remove instead of Add.
Using ContainsKey which only checks for keys.
✗ Incorrect
To add a new key-value pair, use the Add method.
3fill in blank
hardFix the error in the code to retrieve a value by key safely.
DSA C#
if (hashTable.[1](3, out string value)) { Console.WriteLine(value); } else { Console.WriteLine("Key not found"); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using ContainsKey which does not output the value.
Using Add or Remove which modify the dictionary.
✗ Incorrect
TryGetValue tries to get the value for a key safely without throwing an error if the key is missing.
4fill in blank
hardFill both blanks to compute a simple hash index for a key in an array of size 10.
DSA C#
int hashIndex = key [1] 10 [2] 0;
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication or subtraction instead of modulo.
Omitting the addition operator.
✗ Incorrect
The modulo operator (%) gives the remainder to fit the key into array size. Adding zero (+) keeps the value unchanged.
5fill in blank
hardFill all three blanks to create a hash function that sums character codes and uses modulo 16.
DSA C#
int HashFunction(string key) {
int sum = 0;
foreach (char c in key) {
sum [1]= (int)c;
}
return sum [2] [3];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' to add character codes.
Using wrong modulo value or operator.
✗ Incorrect
We add each character code to sum, then use modulo 16 to keep the hash within 0-15.
