Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add a new key-value pair to the hash table using chaining.
DSA C#
public void Add(int key, string value) {
int index = GetHash(key);
if (buckets[index] == null) {
buckets[index] = new LinkedList<KeyValuePair<int, string>>();
}
buckets[index].[1](new KeyValuePair<int, string>(key, value));
}Attempts:
3 left
💡 Hint
Common Mistakes
Using AddFirst adds the element at the start, which is allowed but not the usual chaining approach.
Using Add or Insert causes errors because LinkedList does not have these methods.
✗ Incorrect
We use AddLast to add the new key-value pair at the end of the linked list in the bucket.
2fill in blank
mediumComplete the code to find the bucket index for a given key.
DSA C#
private int GetHash(int key) {
return key [1] buckets.Length;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using division (/) returns a float or int division result, not suitable for index.
Using multiplication (*) or subtraction (-) does not ensure index within array bounds.
✗ Incorrect
The modulo operator (%) gives the remainder, which is used to find the bucket index within array bounds.
3fill in blank
hardFix the error in the code that searches for a key in the linked list bucket.
DSA C#
public string Get(int key) {
int index = GetHash(key);
if (buckets[index] != null) {
foreach (var pair in buckets[index]) {
if (pair.Key [1] key) {
return pair.Value;
}
}
}
return null;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' causes a compile error because it's assignment, not comparison.
Using '!=' or '<' will not find the correct key.
✗ Incorrect
We use '==' to check if the keys are equal to find the correct value.
4fill in blank
hardFill both blanks to remove a key-value pair from the hash table using chaining.
DSA C#
public bool Remove(int key) {
int index = GetHash(key);
if (buckets[index] != null) {
var node = buckets[index].First;
while (node != null) {
if (node.Value.Key [1] key) {
buckets[index].[2](node);
return true;
}
node = node.Next;
}
}
return false;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' in the if condition skips the correct node.
Using RemoveNode causes errors because LinkedList does not have this method; Remove accepts a node.
✗ Incorrect
We check equality with '==' and remove the node using Remove method.
5fill in blank
hardFill all three blanks to update the value for an existing key in the hash table using chaining.
DSA C#
public void Update(int key, string newValue) {
int index = GetHash(key);
if (buckets[index] != null) {
foreach (var node in buckets[index]) {
if (node.Key [1] key) {
var pair = new KeyValuePair<int, string>(node.Key, [2]);
buckets[index].[3](pair);
return;
}
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' in the if condition.
Using old value instead of newValue.
Using Remove instead of AddLast to add the updated pair.
✗ Incorrect
We check equality with '==', create a new pair with newValue, and add it at the end with AddLast.
