diff --git a/hashMapUsingDoubleHashing.js b/hashMapUsingDoubleHashing.js new file mode 100644 index 00000000..4d8e68e5 --- /dev/null +++ b/hashMapUsingDoubleHashing.js @@ -0,0 +1,77 @@ +/** + * 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 and gives the locations to put the key and value pair. + * put can insert a new key, val pair or update the value if there is an existant key. + * get implemented to get the value associated with the key + * remove deletes the entry of the key value pair in the index. + */ + + +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 firstHashLoc = this.parentArray[mainIndex]; + const secondHashIndex = this.hashTwo(key); + return [firstHashLoc, secondHashIndex]; + } + +}; + +/** + * @param {number} key + * @param {number} value + * @return {void} + */ +MyHashMap.prototype.put = function (key, value) { + let [firstHashLoc, secondHashIndex] = this.getFinalLocation(key); + if (!firstHashLoc[secondHashIndex]) { + firstHashLoc[secondHashIndex] = [key, value]; + } else { + firstHashLoc[secondHashIndex][1] = value; + } +}; + +/** + * @param {number} key + * @return {number} + */ +MyHashMap.prototype.get = function (key) { + let [firstHashLoc, secondHashIndex] = this.getFinalLocation(key); + if (!firstHashLoc[secondHashIndex]) return -1; + return firstHashLoc[secondHashIndex][1]; +}; + +/** + * @param {number} key + * @return {void} + */ +MyHashMap.prototype.remove = function (key) { + let [firstHashLoc, secondHashIndex] = this.getFinalLocation(key); + if (!firstHashLoc[secondHashIndex]) return; + // setting null removes both key and val from this location + firstHashLoc[secondHashIndex] = 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/queueDesignUsingStacks.js b/queueDesignUsingStacks.js new file mode 100644 index 00000000..4343a2c8 --- /dev/null +++ b/queueDesignUsingStacks.js @@ -0,0 +1,66 @@ + +/** + * Intuition: + * Here, i am using two stack to create the queue. + * Inside the constructor, i am initializing the in and out stacks. + * Whenever we need to put an element in to the queue, i am pushing that into in stack. + * Whenever we need to pop an element from the queue, i am popping the peak element from outqueue. + * Inside the peak, i am seeing if the out queue is empty or not.if it is empty i am moving all the element available from in stack(pop the last ele) to outstack(push). + * This will allow us to have the first inserted element to be on the top of outstack. + * + * Time complexity: + * + */ + +class MyQueue { + constructor() { + + 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