|
| 1 | +/*Implement a HashMap class without using JavaScript’s built-in objects ({}) or Maps. You are provided a hash() function that takes a string and returns a number (the numbers are mostly unique, but sometimes two different strings will return the same number): |
| 2 | +
|
| 3 | +function hash (string) { |
| 4 | + return string |
| 5 | + .split('') |
| 6 | + .reduce((a, b) => ((a << 5) + a) + b.charCodeAt(0), 5381) |
| 7 | +} |
| 8 | +Your HashMap should support just 2 methods, get, set: |
| 9 | +
|
| 10 | +let map = new HashMap |
| 11 | +map.set('abc', 123) // undefined |
| 12 | +map.set('foo', 'bar') // undefined |
| 13 | +map.set('foo', 'baz') // undefined |
| 14 | +map.get('abc') // 123 |
| 15 | +map.get('foo') // 'baz' |
| 16 | +map.get('def') // undefined*/ |
| 17 | + |
| 18 | +// hash function (provided in the problem) |
| 19 | + |
| 20 | +class HashMap { |
| 21 | + constructor() { |
| 22 | + this.data = [] |
| 23 | + /* Create an empty array whose each index position is a slot or bucket. So this bucket is an array of arrays. So, I will access a value in bucket, which is the inner-array |
| 24 | + bucket[indexInBucket][i] */ |
| 25 | + } |
| 26 | + |
| 27 | + // This hash function was already given in the original problem |
| 28 | + hash(string) { |
| 29 | + return string |
| 30 | + .split('') |
| 31 | + .reduce((a, b) => ((a << 5) + a) + b.charCodeAt(0), 5381) |
| 32 | + } |
| 33 | + |
| 34 | + get(key) { |
| 35 | + let index = hash(key); // First I have to hash the given key to get the index postion in the hashtable |
| 36 | + let bucket = this.data[index]; |
| 37 | + if (!bucket) { |
| 38 | + return undefined; |
| 39 | + } |
| 40 | + |
| 41 | + for ([k, v] of bucket) { |
| 42 | + if (key = k) { |
| 43 | + return v; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /*Function for storing a value in the Hash. In this the "key" is the non-hashed version of index position of the HashTable. So, I have to first get it hashed to create the actual key of the hashTable with which to store the value in the HashTable. |
| 49 | + */ |
| 50 | + set(key, value) { |
| 51 | + let index = hash(key); |
| 52 | + |
| 53 | + // If the hashed-key does not exist yet then create an empty array with that hashed-key |
| 54 | + if(!this.data[index]) { |
| 55 | + this.data[index] = []; |
| 56 | + } |
| 57 | + |
| 58 | + /*"bucket" is the slot or bucket at which I will store the passed-in "value" argument. */ |
| 59 | + let bucket = this.data[index]; |
| 60 | + let indexInBucket = 0; // Initialize the first index no to be searched in bucket with '0' |
| 61 | + |
| 62 | + /*Now before storing the value at the first empty index position that is found >> |
| 63 | + Search for the bucket index position starting from zero. Implement collision-resolution. |
| 64 | + So, first check if there's any value stored at all, in bucket[indexInBucket] >> If true, then check, if this specific key is the one that was assigned to store that value. |
| 65 | + */ |
| 66 | + while (bucket[indexInBucket]) { |
| 67 | + if(bucket[indexInBucket] == key) { |
| 68 | + break |
| 69 | + } |
| 70 | + indexInBucket++ |
| 71 | + } |
| 72 | + // And after I have checked for No-collision, store the value. Note that this code for storing is outside the value of the while loop. |
| 73 | + bucket[indexInBucket] = [key, value]; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// Now test with some key-values - |
| 78 | +// ISSUES - I have to implemnt this test following the best practice file structure. |
| 79 | + |
| 80 | +/*import { test } from 'ava' |
| 81 | +
|
| 82 | +test('HashMap', t => { |
| 83 | + let map = new HashMap |
| 84 | + map.set('abc', 123) |
| 85 | + map.set('foo', 'bar') |
| 86 | + map.set('foo', 'baz') |
| 87 | + t.is(map.get('abc'), 123) |
| 88 | + t.is(map.get('foo'), 'baz') |
| 89 | + t.is(map.get('def'), undefined) |
| 90 | +})*/ |
| 91 | + |
0 commit comments