From 1c3108827d9bed63b80bf184f0d86df99ca858fa Mon Sep 17 00:00:00 2001 From: RamKuppagiri Date: Tue, 23 Dec 2025 18:42:50 -0600 Subject: [PATCH 1/6] 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/6] 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 db38a7ff4f5333e880c12e35e05e3682a9113108 Mon Sep 17 00:00:00 2001 From: RamKuppagiri Date: Thu, 25 Dec 2025 19:20:30 -0600 Subject: [PATCH 3/6] queueDesign --- queueDesignUsingStacks.js | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 queueDesignUsingStacks.js diff --git a/queueDesignUsingStacks.js b/queueDesignUsingStacks.js new file mode 100644 index 00000000..9cb0fe17 --- /dev/null +++ b/queueDesignUsingStacks.js @@ -0,0 +1,53 @@ + +class MyQueue { + constructor() { + // Not allocating space as the main way to test whether it is empty or not is by checking it's length. If we allocate the space, the length always holds some value and even though there is no data. + this.inStack = []; + this.outStack = []; + } +}; + +/** + * @param {number} x + * @return {void} + */ +MyQueue.prototype.push = function (x) { + this.inStack.push(x); +}; + +/** + * @return {number} + */ +MyQueue.prototype.pop = function () { + this.peek(); + return this.outStack.pop(); +}; + +/** + * @return {number} + */ +MyQueue.prototype.peek = function () { + if (!this.outStack.length) { + while (this.inStack.length > 0) { + this.outStack.push(this.inStack.pop()); + } + } + if(!this.outStack.length) return; + return this.outStack[this.outStack.length - 1]; +}; + +/** + * @return {boolean} + */ +MyQueue.prototype.empty = function () { + return this.outStack.length === 0 && this.inStack.length === 0; +}; + +/** + * Your MyQueue object will be instantiated and called as such: + * var obj = new MyQueue() + * obj.push(x) + * var param_2 = obj.pop() + * var param_3 = obj.peek() + * var param_4 = obj.empty() + */ \ No newline at end of file From 932a4c5ab046b703f0eff4ccb13ef390c1fdafab Mon Sep 17 00:00:00 2001 From: dbkuppagiri <32444173+dbkuppagiri@users.noreply.github.com> Date: Thu, 25 Dec 2025 19:23:17 -0600 Subject: [PATCH 4/6] Delete hashSetDesign.js --- hashSetDesign.js | 65 ------------------------------------------------ 1 file changed, 65 deletions(-) delete mode 100644 hashSetDesign.js 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 From 7abee02c679f13cbe5bec32fa68359504533c1a0 Mon Sep 17 00:00:00 2001 From: dbkuppagiri <32444173+dbkuppagiri@users.noreply.github.com> Date: Thu, 25 Dec 2025 19:23:35 -0600 Subject: [PATCH 5/6] Delete minStackDesign.js --- minStackDesign.js | 66 ----------------------------------------------- 1 file changed, 66 deletions(-) delete mode 100644 minStackDesign.js 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 d90f4c2b104d5fcdeebd2cc218e1516e9cd86dbd Mon Sep 17 00:00:00 2001 From: RamKuppagiri Date: Thu, 25 Dec 2025 19:31:12 -0600 Subject: [PATCH 6/6] intution added --- queueDesignUsingStacks.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/queueDesignUsingStacks.js b/queueDesignUsingStacks.js index 9cb0fe17..c0e279c0 100644 --- a/queueDesignUsingStacks.js +++ b/queueDesignUsingStacks.js @@ -1,7 +1,15 @@ +/** + * Intution: + * Here i am using in and out stack concept. + * Outstack contains the data that are in the reverse order of in stack, which will help us pop the firdt element that gets in + * Not allocating space to the array in the constructor, as the main way to test whether it is empty or not is by checking it's length. If we allocate the space, the length always holds some value and even though there is no data. + * + */ + class MyQueue { constructor() { - // Not allocating space as the main way to test whether it is empty or not is by checking it's length. If we allocate the space, the length always holds some value and even though there is no data. + this.inStack = []; this.outStack = []; }