Complete the code to declare the internal storage array for the HashMap.
private buckets: Array<Array<[string, any]>> = new Array([1]);The internal storage is an array with a fixed size, commonly 16 buckets initially.
Complete the code to compute the hash index for a given key.
const index = this.hash(key) [1] this.buckets.length;The modulo operator (%) is used to ensure the hash fits within the bucket array size.
Fix the error in the put method to correctly update an existing key's value.
for (let i = 0; i < bucket.length; i++) { if (bucket[i][0] === key) { bucket[i][[1]] = value; return; } }
The value is stored at index 1 in the key-value pair array [key, value].
Fill both blanks to correctly retrieve a value by key from the HashMap.
const bucket = this.buckets[[1]]; if (!bucket) return undefined; for (const pair of bucket) { if (pair[0] === [2]) return pair[1]; } return undefined;
We use the computed index to get the bucket and the key to compare with pairs.
Fill all three blanks to complete the hash function that sums character codes.
hash(key: string): number {
let hash = 0;
for (let i = 0; i < [1]; i++) {
hash += key.charCodeAt([2]);
}
return hash [3] 31;
}The loop runs over the key length, uses index i for charCodeAt, and modulo 31 to limit hash size.
