Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the array that stores the buckets in the HashMap class.
DSA C#
private LinkedList<KeyValuePair<TKey, TValue>>[] [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using a generic name like 'items' which is less descriptive.
Using singular form instead of plural.
✗ Incorrect
The array that stores the buckets is commonly named 'buckets' to represent the storage slots for key-value pairs.
2fill in blank
mediumComplete the code to compute the index for a given key in the HashMap.
DSA C#
int index = Math.Abs(key.GetHashCode()) [1] buckets.Length;Attempts:
3 left
💡 Hint
Common Mistakes
Using division '/' which returns a quotient, not an index.
Using multiplication '*' which can exceed array bounds.
✗ Incorrect
The modulo operator '%' is used to ensure the index fits within the array bounds.
3fill in blank
hardFix the error in the Add method to correctly add a new key-value pair to the bucket.
DSA C#
if (buckets[index] == null) { buckets[index] = new LinkedList<KeyValuePair<TKey, TValue>>(); } buckets[index].[1](new KeyValuePair<TKey, TValue>(key, value));
Attempts:
3 left
💡 Hint
Common Mistakes
Using AddFirst which adds at the start and may affect order.
Using AddBefore or AddAfter without a reference node.
✗ Incorrect
AddLast adds the new key-value pair at the end of the linked list, which is the standard approach.
4fill in blank
hardFill both blanks to complete the Remove method that deletes a key-value pair by key.
DSA C#
int index = Math.Abs(key.GetHashCode()) [1] buckets.Length; if (buckets[index] != null) { var current = buckets[index].First; while (current != null) { if (current.Value.Key.Equals(key)) { buckets[index].[2](current); return true; } current = current.Next; } } return false;
Attempts:
3 left
💡 Hint
Common Mistakes
Using division '/' instead of modulo for index calculation.
Using RemoveFirst which removes the first node regardless of key.
✗ Incorrect
The modulo operator '%' calculates the index, and Remove removes the node from the linked list.
5fill in blank
hardFill all three blanks to complete the TryGetValue method that retrieves a value by key.
DSA C#
int index = Math.Abs(key.GetHashCode()) [1] buckets.Length; if (buckets[index] != null) { foreach (var pair in buckets[index]) { if (pair.Key.[2](key)) { value = pair.[3]; return true; } } } value = default; return false;
Attempts:
3 left
💡 Hint
Common Mistakes
Using Contains which is not a method on keys.
Using incorrect property names for accessing value.
✗ Incorrect
Modulo '%' calculates the index, Equals compares keys, and Value accesses the stored value.
