From 1c3108827d9bed63b80bf184f0d86df99ca858fa Mon Sep 17 00:00:00 2001 From: RamKuppagiri Date: Tue, 23 Dec 2025 18:42:50 -0600 Subject: [PATCH 1/4] hashset design --- hashSetDesign.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 hashSetDesign.js diff --git a/hashSetDesign.js b/hashSetDesign.js new file mode 100644 index 00000000..5fc8d772 --- /dev/null +++ b/hashSetDesign.js @@ -0,0 +1,65 @@ + +/** + * Intution: + * I am using bucket + arrays concept that i learnt online, + * Using hash function to give an index for the given key which helps us identifying the bucket during add/ remove operations + * chaining (each index holds an array) helps us having store all the values that belongs to that hash index without conflicts. + + */ + +class MyHashSet { + constructor() { + + this.numerOfBuckets = 1000; + this.buckets = Array.from({ + length: this.numerOfBuckets + }, + () => []); + } + + hashing = (key) => { + return key % this.numerOfBuckets; + } +}; + + +/** + * @param {number} key + * @return {void} + */ +MyHashSet.prototype.add = function (key) { + const hashIndex = this.hashing(key); + const currBucket = this.buckets[hashIndex]; + if (!currBucket?.includes(key)) + currBucket.push(key); +}; + +/** + * @param {number} key + * @return {void} + */ +MyHashSet.prototype.remove = function (key) { + const hashIndex = this.hashing(key); + const currBucket = this.buckets[hashIndex]; + if (currBucket.includes(key)) { + const indexOfTheKey = currBucket.indexOf(key); + currBucket.splice(indexOfTheKey, 1); + } +}; + +/** + * @param {number} key + * @return {boolean} + */ +MyHashSet.prototype.contains = function (key) { + const hashIndex = this.hashing(key); + return this.buckets[hashIndex].includes(key); +}; + +/** + * Your MyHashSet object will be instantiated and called as such: + * var obj = new MyHashSet() + * obj.add(key) + * obj.remove(key) + * var param_3 = obj.contains(key) + */ \ No newline at end of file From a10ceb79b2b5a2eda89a5fa1c36bc388083d59cf Mon Sep 17 00:00:00 2001 From: RamKuppagiri Date: Wed, 24 Dec 2025 19:29:49 -0600 Subject: [PATCH 2/4] minStack --- minStackDesign.js | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 minStackDesign.js diff --git a/minStackDesign.js b/minStackDesign.js new file mode 100644 index 00000000..0b245d3c --- /dev/null +++ b/minStackDesign.js @@ -0,0 +1,66 @@ + +/** + * I am using two stacks here + * One to track the values that we are pushing and popping, The other one is to track the minVal at that point of time. + * Reduced more space by pushing the minVal into minStack if and only if the current val that we are pushing into stack is less than the minStack peek val. + * + */ + +class MinStack { + constructor(){ + this.minVal = Infinity; + this.stack = []; + this.minStack = []; + this.minStack.push(this.minVal); + } + }; + + /** + * @param {number} val + * @return {void} + */ + MinStack.prototype.push = function(val) { + // compare the incoming value with the minVal and update the minVal if needed + if(val <= this.minVal){ + this.minVal = val; + // push the minVal on the minStack + this.minStack.push(val); + } + // push the val on to stack + this.stack.push(val); + }; + + /** + * @return {void} + */ + MinStack.prototype.pop = function() { + const currVal = this.stack.pop(); + if(currVal === this.getMin()){ + this.minStack.pop(); + } + this.minVal = this.minStack[this.minStack.length - 1]; + return currVal; + }; + + /** + * @return {number} + */ + MinStack.prototype.top = function() { + return this.stack[this.stack.length - 1]; + }; + + /** + * @return {number} + */ + MinStack.prototype.getMin = function() { + return this.minStack[this.minStack.length - 1]; + }; + + /** + * Your MinStack object will be instantiated and called as such: + * var obj = new MinStack() + * obj.push(val) + * obj.pop() + * var param_3 = obj.top() + * var param_4 = obj.getMin() + */ \ No newline at end of file From 6e4ef7b8a13e1a8ac157fbed807310550633c69a Mon Sep 17 00:00:00 2001 From: RamKuppagiri Date: Thu, 25 Dec 2025 20:10:35 -0600 Subject: [PATCH 3/4] hashMapDesign --- hashMapUsingDoubleHashing.js | 67 ++++++++++++++++++++++++++++++++++++ hashSetDesign.js | 65 ---------------------------------- minStackDesign.js | 66 ----------------------------------- 3 files changed, 67 insertions(+), 131 deletions(-) create mode 100644 hashMapUsingDoubleHashing.js delete mode 100644 hashSetDesign.js delete mode 100644 minStackDesign.js diff --git a/hashMapUsingDoubleHashing.js b/hashMapUsingDoubleHashing.js new file mode 100644 index 00000000..351ed79a --- /dev/null +++ b/hashMapUsingDoubleHashing.js @@ -0,0 +1,67 @@ + +class MyHashMap { + constructor() { + this.bucketSize = 1000; + this.parentArray = Array.from({ + length: this.bucketSize + }, () => []); + } + + hashOne = (key) => { + return key % this.bucketSize; + } + + hashTwo = (key) => { + return Math.floor(key / this.bucketSize); + } + + getFinalLocation = (key) => { + const mainIndex = this.hashOne(key); + const currSlot = this.parentArray[mainIndex]; + const childIndex = this.hashTwo(key); + return [currSlot, childIndex]; + } + +}; + +/** + * @param {number} key + * @param {number} value + * @return {void} + */ +MyHashMap.prototype.put = function (key, value) { + let [currSlot, childIndex] = this.getFinalLocation(key); + if (!currSlot[childIndex]) { + currSlot[childIndex] = [key, value]; + } else { + currSlot[childIndex][1] = value; + } +}; + +/** + * @param {number} key + * @return {number} + */ +MyHashMap.prototype.get = function (key) { + let [currSlot, childIndex] = this.getFinalLocation(key); + if (!currSlot[childIndex]) return -1; + return currSlot[childIndex][1]; +}; + +/** + * @param {number} key + * @return {void} + */ +MyHashMap.prototype.remove = function (key) { + let [currSlot, childIndex] = this.getFinalLocation(key); + if (!currSlot[childIndex]) return; + currSlot[childIndex] = null; +}; + +/** + * Your MyHashMap object will be instantiated and called as such: + * var obj = new MyHashMap() + * obj.put(key,value) + * var param_2 = obj.get(key) + * obj.remove(key) + */ diff --git a/hashSetDesign.js b/hashSetDesign.js deleted file mode 100644 index 5fc8d772..00000000 --- a/hashSetDesign.js +++ /dev/null @@ -1,65 +0,0 @@ - -/** - * Intution: - * I am using bucket + arrays concept that i learnt online, - * Using hash function to give an index for the given key which helps us identifying the bucket during add/ remove operations - * chaining (each index holds an array) helps us having store all the values that belongs to that hash index without conflicts. - - */ - -class MyHashSet { - constructor() { - - this.numerOfBuckets = 1000; - this.buckets = Array.from({ - length: this.numerOfBuckets - }, - () => []); - } - - hashing = (key) => { - return key % this.numerOfBuckets; - } -}; - - -/** - * @param {number} key - * @return {void} - */ -MyHashSet.prototype.add = function (key) { - const hashIndex = this.hashing(key); - const currBucket = this.buckets[hashIndex]; - if (!currBucket?.includes(key)) - currBucket.push(key); -}; - -/** - * @param {number} key - * @return {void} - */ -MyHashSet.prototype.remove = function (key) { - const hashIndex = this.hashing(key); - const currBucket = this.buckets[hashIndex]; - if (currBucket.includes(key)) { - const indexOfTheKey = currBucket.indexOf(key); - currBucket.splice(indexOfTheKey, 1); - } -}; - -/** - * @param {number} key - * @return {boolean} - */ -MyHashSet.prototype.contains = function (key) { - const hashIndex = this.hashing(key); - return this.buckets[hashIndex].includes(key); -}; - -/** - * Your MyHashSet object will be instantiated and called as such: - * var obj = new MyHashSet() - * obj.add(key) - * obj.remove(key) - * var param_3 = obj.contains(key) - */ \ No newline at end of file diff --git a/minStackDesign.js b/minStackDesign.js deleted file mode 100644 index 0b245d3c..00000000 --- a/minStackDesign.js +++ /dev/null @@ -1,66 +0,0 @@ - -/** - * I am using two stacks here - * One to track the values that we are pushing and popping, The other one is to track the minVal at that point of time. - * Reduced more space by pushing the minVal into minStack if and only if the current val that we are pushing into stack is less than the minStack peek val. - * - */ - -class MinStack { - constructor(){ - this.minVal = Infinity; - this.stack = []; - this.minStack = []; - this.minStack.push(this.minVal); - } - }; - - /** - * @param {number} val - * @return {void} - */ - MinStack.prototype.push = function(val) { - // compare the incoming value with the minVal and update the minVal if needed - if(val <= this.minVal){ - this.minVal = val; - // push the minVal on the minStack - this.minStack.push(val); - } - // push the val on to stack - this.stack.push(val); - }; - - /** - * @return {void} - */ - MinStack.prototype.pop = function() { - const currVal = this.stack.pop(); - if(currVal === this.getMin()){ - this.minStack.pop(); - } - this.minVal = this.minStack[this.minStack.length - 1]; - return currVal; - }; - - /** - * @return {number} - */ - MinStack.prototype.top = function() { - return this.stack[this.stack.length - 1]; - }; - - /** - * @return {number} - */ - MinStack.prototype.getMin = function() { - return this.minStack[this.minStack.length - 1]; - }; - - /** - * Your MinStack object will be instantiated and called as such: - * var obj = new MinStack() - * obj.push(val) - * obj.pop() - * var param_3 = obj.top() - * var param_4 = obj.getMin() - */ \ No newline at end of file From d90df04d8b17a0408984145ac0459c2e17380f42 Mon Sep 17 00:00:00 2001 From: RamKuppagiri Date: Thu, 25 Dec 2025 20:12:58 -0600 Subject: [PATCH 4/4] intution added --- hashMapUsingDoubleHashing.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/hashMapUsingDoubleHashing.js b/hashMapUsingDoubleHashing.js index 351ed79a..8935524c 100644 --- a/hashMapUsingDoubleHashing.js +++ b/hashMapUsingDoubleHashing.js @@ -1,3 +1,9 @@ +/** + * I used double hashing technique here + * First hash function uses mod and second hash function uses division + * Created a method called getFinalLocation which uses both hashOne and hashTwo functions + */ + class MyHashMap { constructor() {