From b73602fa671d92338f709a41fd404ba545c81eeb Mon Sep 17 00:00:00 2001 From: tmujje Date: Fri, 23 Apr 2021 06:45:23 -0400 Subject: [PATCH 1/3] Design-2 Solutions - Please review --- MyHashSet.cs | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++ MyQueue.cs | 71 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 MyHashSet.cs create mode 100644 MyQueue.cs diff --git a/MyHashSet.cs b/MyHashSet.cs new file mode 100644 index 00000000..838d1085 --- /dev/null +++ b/MyHashSet.cs @@ -0,0 +1,87 @@ +namespace CustomDataStructures +{ + public class MyHashSet + { + // Did this code successfully run on Leetcode : Yes + // Any problem you faced while coding this : No + + private bool[][] arr; + private int buckets; + private int bucketItems; + + /** Initialize your data structure here. */ + public MyHashSet() + { + buckets = 1000; + bucketItems = 1000; + arr = new bool[buckets][]; + } + + //Hash function for caluclating index of the buckets + public int Bucket(int key) + { + return key % buckets; + } + + //Hash function for caluclating index of the bucket items (Nested index - index for the nested structure) + public int BucketItem(int key) + { + return key / bucketItems; + } + + //Time Complexity - O(1) + //Space Complexity - O(n) in this method - Since we have to just allocated BucketItems here + public void Add(int key) + { + var bucket = Bucket(key); + var bucketItem = BucketItem(key); + if (arr[bucket] == null) + { + if (bucket == 0) + { + // If the key is 10^6, then our hash function returns 1000 for the 0th index, + // where as we can only store 0 to 999 bucketItems, for this reason I am checking if the first index is 0, + // then allocate 1001 elements so that my index can go upto 1000 for the bucket + arr[bucket] = new bool[bucketItems + 1]; + } + else + { + arr[bucket] = new bool[bucketItems]; + } + } + arr[bucket][bucketItem] = true; + } + + //Time Complexity - O(1) + //Space Complexity - O(1) + public void Remove(int key) + { + var bucket = Bucket(key); + var bucketItem = BucketItem(key); + if (arr[bucket] == null) return; + arr[bucket][bucketItem] = false; + } + + //Time Complexity - O(1) + //Space Complexity - O(1) + /** Returns true if this set contains the specified element */ + public bool Contains(int key) + { + var bucket = Bucket(key); + var bucketItem = BucketItem(key); + if (arr[bucket] == null) + { + return false; + } + return arr[bucket][bucketItem]; + } + } + + /** + * Your MyHashSet object will be instantiated and called as such: + * MyHashSet obj = new MyHashSet(); + * obj.Add(key); + * obj.Remove(key); + * bool param_3 = obj.Contains(key); + */ +} diff --git a/MyQueue.cs b/MyQueue.cs new file mode 100644 index 00000000..e20ecc14 --- /dev/null +++ b/MyQueue.cs @@ -0,0 +1,71 @@ +using System.Collections.Generic; + +namespace CustomDataStructures +{ + public class MyQueue + { + // Did this code successfully run on Leetcode : Yes + // Any problem you faced while coding this : No + private Stack firstStack; + private Stack secondStack; + + /** Initialize your data structure here. */ + public MyQueue() + { + firstStack = new Stack(); + secondStack = new Stack(); + + } + + //Time Complexity - O(1) + //Space Complexity - O(1) + /** Push element x to the back of queue. */ + public void Push(int x) + { + firstStack.Push(x); + } + + //Time Complexity - O(n) - As part of peek, we are shifting elements from first stack to second stack + //Space Complexity - O(2n) which boils down to O(n) - As we are utilizing two stacks as part of peek + /** Removes the element from in front of queue and returns that element. */ + public int Pop() + { + Peek(); // This allows the flipping of order to FIFO when we move elements from first stack to second stack + return secondStack.Pop(); + } + + //Time Complexity - O(n) - As part of peek, we are shifting elements from first stack to second stack + //Space Complexity - O(2n) which boils down to O(n) - As we are utilizing two stacks as part of peek + /** Get the front element. */ + public int Peek() + { + // Transfer elements of first stack to second stack to flip the order + if (secondStack.Count == 0) + { + while (firstStack.Count != 0) + { + secondStack.Push(firstStack.Pop()); + } + } + + return secondStack.Peek(); + } + + //Time Complexity - O(1) + //Space Complexity - O(1) + /** Returns whether the queue is empty. */ + public bool Empty() + { + return firstStack.Count == 0 && secondStack.Count == 0; + } + } + + /** + * Your MyQueue object will be instantiated and called as such: + * MyQueue obj = new MyQueue(); + * obj.Push(x); + * int param_2 = obj.Pop(); + * int param_3 = obj.Peek(); + * bool param_4 = obj.Empty(); + */ +} From e7c730416d09091d5c69123df6212bcecf3dda74 Mon Sep 17 00:00:00 2001 From: pubali25 Date: Sun, 2 Aug 2026 22:16:32 -0400 Subject: [PATCH 2/3] Complete Design-2 assignment Remove older implementation and add java implementation for HashSet and Queue using Stacks --- ImplementHashMap.java | 81 +++++++++++++++++++++++++++++++ ImplementQueueUsingStacks.java | 50 +++++++++++++++++++ MyHashSet.cs | 87 ---------------------------------- MyQueue.cs | 71 --------------------------- 4 files changed, 131 insertions(+), 158 deletions(-) create mode 100644 ImplementHashMap.java create mode 100644 ImplementQueueUsingStacks.java delete mode 100644 MyHashSet.cs delete mode 100644 MyQueue.cs diff --git a/ImplementHashMap.java b/ImplementHashMap.java new file mode 100644 index 00000000..3fc1da5d --- /dev/null +++ b/ImplementHashMap.java @@ -0,0 +1,81 @@ +//Approach: Similar to HashSet implementation try using double-hashing method +// to handle collisions + +// Time Complexity: O(1) +// Space Complexity: O(1) + +class MyHashMap { + + int[][] arr; + int buckets; + int bucketItems; + + public MyHashMap() { + buckets = 1000; + bucketItems = 1000; + arr = new int[buckets][]; + } + + public int getPrimaryHash(int key) + { + return key % buckets; + } + + public int getSecondaryHash(int key) + { + return key / bucketItems; + } + + public void put(int key, int value) { + var firstIndex = getPrimaryHash(key); + var secondIndex = getSecondaryHash(key); + if (arr[firstIndex] == null) + { + if (firstIndex == 0) + { + arr[firstIndex] = new int[bucketItems + 1]; + } + else + { + arr[firstIndex] = new int[bucketItems]; + } + } + if (value == 0) + { + arr[firstIndex][secondIndex] = value - 2; + } + else + { + arr[firstIndex][secondIndex] = value; + } + } + + public int get(int key) { + var firstIndex = getPrimaryHash(key); + var secondIndex = getSecondaryHash(key); + + if (arr[firstIndex] == null || arr[firstIndex][secondIndex] == 0) + { + return -1; + } + if (arr[firstIndex][secondIndex] == -2) + { + return 0; + } + return arr[firstIndex][secondIndex]; + } + + public void remove(int key) { + var firstIndex = getPrimaryHash(key); + var secondIndex = getSecondaryHash(key); + if (arr[firstIndex] == null || arr[firstIndex][secondIndex] == -1) return; + arr[firstIndex][secondIndex] = -1; + } +} +/** + * Your MyHashMap object will be instantiated and called as such: + * MyHashMap obj = new MyHashMap(); + * obj.put(key,value); + * int param_2 = obj.get(key); + * obj.remove(key); + */ \ No newline at end of file diff --git a/ImplementQueueUsingStacks.java b/ImplementQueueUsingStacks.java new file mode 100644 index 00000000..b87e74cd --- /dev/null +++ b/ImplementQueueUsingStacks.java @@ -0,0 +1,50 @@ +//Approach: The idea is to hold elements in one stack, until a pop() or peek() happens and copy the elements from first stack to another and then pop() or peek() + +// Time Complexity for Pop() and Peek() is O(n) (as linear traversal is involved) +// Time Complexity for Push() and empty() is O(1) + +// Space Complexity: O(n), as we have to maintain another stack to temporarily hold elements for pop() and peek() operations + +class MyQueue { + + Stack firstStack; + Stack secondStack; + + public MyQueue() { + firstStack = new Stack<>(); + secondStack = new Stack<>(); + } + + public void push(int x) { + firstStack.push(x); + } + + public int pop() { + peek(); + return secondStack.pop(); + } + + public int peek() { + if(secondStack.size() == 0){ + while(firstStack.size() != 0) + { + secondStack.push(firstStack.pop()); + } + } + + return secondStack.peek(); + } + + public boolean empty() { + return firstStack.size() == 0 && secondStack.size() == 0; + } +} + +/** + * Your MyQueue object will be instantiated and called as such: + * MyQueue obj = new MyQueue(); + * obj.push(x); + * int param_2 = obj.pop(); + * int param_3 = obj.peek(); + * boolean param_4 = obj.empty(); + */ \ No newline at end of file diff --git a/MyHashSet.cs b/MyHashSet.cs deleted file mode 100644 index 838d1085..00000000 --- a/MyHashSet.cs +++ /dev/null @@ -1,87 +0,0 @@ -namespace CustomDataStructures -{ - public class MyHashSet - { - // Did this code successfully run on Leetcode : Yes - // Any problem you faced while coding this : No - - private bool[][] arr; - private int buckets; - private int bucketItems; - - /** Initialize your data structure here. */ - public MyHashSet() - { - buckets = 1000; - bucketItems = 1000; - arr = new bool[buckets][]; - } - - //Hash function for caluclating index of the buckets - public int Bucket(int key) - { - return key % buckets; - } - - //Hash function for caluclating index of the bucket items (Nested index - index for the nested structure) - public int BucketItem(int key) - { - return key / bucketItems; - } - - //Time Complexity - O(1) - //Space Complexity - O(n) in this method - Since we have to just allocated BucketItems here - public void Add(int key) - { - var bucket = Bucket(key); - var bucketItem = BucketItem(key); - if (arr[bucket] == null) - { - if (bucket == 0) - { - // If the key is 10^6, then our hash function returns 1000 for the 0th index, - // where as we can only store 0 to 999 bucketItems, for this reason I am checking if the first index is 0, - // then allocate 1001 elements so that my index can go upto 1000 for the bucket - arr[bucket] = new bool[bucketItems + 1]; - } - else - { - arr[bucket] = new bool[bucketItems]; - } - } - arr[bucket][bucketItem] = true; - } - - //Time Complexity - O(1) - //Space Complexity - O(1) - public void Remove(int key) - { - var bucket = Bucket(key); - var bucketItem = BucketItem(key); - if (arr[bucket] == null) return; - arr[bucket][bucketItem] = false; - } - - //Time Complexity - O(1) - //Space Complexity - O(1) - /** Returns true if this set contains the specified element */ - public bool Contains(int key) - { - var bucket = Bucket(key); - var bucketItem = BucketItem(key); - if (arr[bucket] == null) - { - return false; - } - return arr[bucket][bucketItem]; - } - } - - /** - * Your MyHashSet object will be instantiated and called as such: - * MyHashSet obj = new MyHashSet(); - * obj.Add(key); - * obj.Remove(key); - * bool param_3 = obj.Contains(key); - */ -} diff --git a/MyQueue.cs b/MyQueue.cs deleted file mode 100644 index e20ecc14..00000000 --- a/MyQueue.cs +++ /dev/null @@ -1,71 +0,0 @@ -using System.Collections.Generic; - -namespace CustomDataStructures -{ - public class MyQueue - { - // Did this code successfully run on Leetcode : Yes - // Any problem you faced while coding this : No - private Stack firstStack; - private Stack secondStack; - - /** Initialize your data structure here. */ - public MyQueue() - { - firstStack = new Stack(); - secondStack = new Stack(); - - } - - //Time Complexity - O(1) - //Space Complexity - O(1) - /** Push element x to the back of queue. */ - public void Push(int x) - { - firstStack.Push(x); - } - - //Time Complexity - O(n) - As part of peek, we are shifting elements from first stack to second stack - //Space Complexity - O(2n) which boils down to O(n) - As we are utilizing two stacks as part of peek - /** Removes the element from in front of queue and returns that element. */ - public int Pop() - { - Peek(); // This allows the flipping of order to FIFO when we move elements from first stack to second stack - return secondStack.Pop(); - } - - //Time Complexity - O(n) - As part of peek, we are shifting elements from first stack to second stack - //Space Complexity - O(2n) which boils down to O(n) - As we are utilizing two stacks as part of peek - /** Get the front element. */ - public int Peek() - { - // Transfer elements of first stack to second stack to flip the order - if (secondStack.Count == 0) - { - while (firstStack.Count != 0) - { - secondStack.Push(firstStack.Pop()); - } - } - - return secondStack.Peek(); - } - - //Time Complexity - O(1) - //Space Complexity - O(1) - /** Returns whether the queue is empty. */ - public bool Empty() - { - return firstStack.Count == 0 && secondStack.Count == 0; - } - } - - /** - * Your MyQueue object will be instantiated and called as such: - * MyQueue obj = new MyQueue(); - * obj.Push(x); - * int param_2 = obj.Pop(); - * int param_3 = obj.Peek(); - * bool param_4 = obj.Empty(); - */ -} From 1b7ed5628dc67ea02627d70db95b179e508c0ecb Mon Sep 17 00:00:00 2001 From: tejbharath Date: Sun, 2 Aug 2026 22:19:00 -0400 Subject: [PATCH 3/3] Complete Design-2 assignment Remove older implementation and add java implementation for HashSet and Queue using Stacks - Updated author as it is configured wrongly --- ImplementHashMap.java | 81 +++++++++++++++++++++++++++++++ ImplementQueueUsingStacks.java | 50 +++++++++++++++++++ MyHashSet.cs | 87 ---------------------------------- MyQueue.cs | 71 --------------------------- 4 files changed, 131 insertions(+), 158 deletions(-) create mode 100644 ImplementHashMap.java create mode 100644 ImplementQueueUsingStacks.java delete mode 100644 MyHashSet.cs delete mode 100644 MyQueue.cs diff --git a/ImplementHashMap.java b/ImplementHashMap.java new file mode 100644 index 00000000..3fc1da5d --- /dev/null +++ b/ImplementHashMap.java @@ -0,0 +1,81 @@ +//Approach: Similar to HashSet implementation try using double-hashing method +// to handle collisions + +// Time Complexity: O(1) +// Space Complexity: O(1) + +class MyHashMap { + + int[][] arr; + int buckets; + int bucketItems; + + public MyHashMap() { + buckets = 1000; + bucketItems = 1000; + arr = new int[buckets][]; + } + + public int getPrimaryHash(int key) + { + return key % buckets; + } + + public int getSecondaryHash(int key) + { + return key / bucketItems; + } + + public void put(int key, int value) { + var firstIndex = getPrimaryHash(key); + var secondIndex = getSecondaryHash(key); + if (arr[firstIndex] == null) + { + if (firstIndex == 0) + { + arr[firstIndex] = new int[bucketItems + 1]; + } + else + { + arr[firstIndex] = new int[bucketItems]; + } + } + if (value == 0) + { + arr[firstIndex][secondIndex] = value - 2; + } + else + { + arr[firstIndex][secondIndex] = value; + } + } + + public int get(int key) { + var firstIndex = getPrimaryHash(key); + var secondIndex = getSecondaryHash(key); + + if (arr[firstIndex] == null || arr[firstIndex][secondIndex] == 0) + { + return -1; + } + if (arr[firstIndex][secondIndex] == -2) + { + return 0; + } + return arr[firstIndex][secondIndex]; + } + + public void remove(int key) { + var firstIndex = getPrimaryHash(key); + var secondIndex = getSecondaryHash(key); + if (arr[firstIndex] == null || arr[firstIndex][secondIndex] == -1) return; + arr[firstIndex][secondIndex] = -1; + } +} +/** + * Your MyHashMap object will be instantiated and called as such: + * MyHashMap obj = new MyHashMap(); + * obj.put(key,value); + * int param_2 = obj.get(key); + * obj.remove(key); + */ \ No newline at end of file diff --git a/ImplementQueueUsingStacks.java b/ImplementQueueUsingStacks.java new file mode 100644 index 00000000..b87e74cd --- /dev/null +++ b/ImplementQueueUsingStacks.java @@ -0,0 +1,50 @@ +//Approach: The idea is to hold elements in one stack, until a pop() or peek() happens and copy the elements from first stack to another and then pop() or peek() + +// Time Complexity for Pop() and Peek() is O(n) (as linear traversal is involved) +// Time Complexity for Push() and empty() is O(1) + +// Space Complexity: O(n), as we have to maintain another stack to temporarily hold elements for pop() and peek() operations + +class MyQueue { + + Stack firstStack; + Stack secondStack; + + public MyQueue() { + firstStack = new Stack<>(); + secondStack = new Stack<>(); + } + + public void push(int x) { + firstStack.push(x); + } + + public int pop() { + peek(); + return secondStack.pop(); + } + + public int peek() { + if(secondStack.size() == 0){ + while(firstStack.size() != 0) + { + secondStack.push(firstStack.pop()); + } + } + + return secondStack.peek(); + } + + public boolean empty() { + return firstStack.size() == 0 && secondStack.size() == 0; + } +} + +/** + * Your MyQueue object will be instantiated and called as such: + * MyQueue obj = new MyQueue(); + * obj.push(x); + * int param_2 = obj.pop(); + * int param_3 = obj.peek(); + * boolean param_4 = obj.empty(); + */ \ No newline at end of file diff --git a/MyHashSet.cs b/MyHashSet.cs deleted file mode 100644 index 838d1085..00000000 --- a/MyHashSet.cs +++ /dev/null @@ -1,87 +0,0 @@ -namespace CustomDataStructures -{ - public class MyHashSet - { - // Did this code successfully run on Leetcode : Yes - // Any problem you faced while coding this : No - - private bool[][] arr; - private int buckets; - private int bucketItems; - - /** Initialize your data structure here. */ - public MyHashSet() - { - buckets = 1000; - bucketItems = 1000; - arr = new bool[buckets][]; - } - - //Hash function for caluclating index of the buckets - public int Bucket(int key) - { - return key % buckets; - } - - //Hash function for caluclating index of the bucket items (Nested index - index for the nested structure) - public int BucketItem(int key) - { - return key / bucketItems; - } - - //Time Complexity - O(1) - //Space Complexity - O(n) in this method - Since we have to just allocated BucketItems here - public void Add(int key) - { - var bucket = Bucket(key); - var bucketItem = BucketItem(key); - if (arr[bucket] == null) - { - if (bucket == 0) - { - // If the key is 10^6, then our hash function returns 1000 for the 0th index, - // where as we can only store 0 to 999 bucketItems, for this reason I am checking if the first index is 0, - // then allocate 1001 elements so that my index can go upto 1000 for the bucket - arr[bucket] = new bool[bucketItems + 1]; - } - else - { - arr[bucket] = new bool[bucketItems]; - } - } - arr[bucket][bucketItem] = true; - } - - //Time Complexity - O(1) - //Space Complexity - O(1) - public void Remove(int key) - { - var bucket = Bucket(key); - var bucketItem = BucketItem(key); - if (arr[bucket] == null) return; - arr[bucket][bucketItem] = false; - } - - //Time Complexity - O(1) - //Space Complexity - O(1) - /** Returns true if this set contains the specified element */ - public bool Contains(int key) - { - var bucket = Bucket(key); - var bucketItem = BucketItem(key); - if (arr[bucket] == null) - { - return false; - } - return arr[bucket][bucketItem]; - } - } - - /** - * Your MyHashSet object will be instantiated and called as such: - * MyHashSet obj = new MyHashSet(); - * obj.Add(key); - * obj.Remove(key); - * bool param_3 = obj.Contains(key); - */ -} diff --git a/MyQueue.cs b/MyQueue.cs deleted file mode 100644 index e20ecc14..00000000 --- a/MyQueue.cs +++ /dev/null @@ -1,71 +0,0 @@ -using System.Collections.Generic; - -namespace CustomDataStructures -{ - public class MyQueue - { - // Did this code successfully run on Leetcode : Yes - // Any problem you faced while coding this : No - private Stack firstStack; - private Stack secondStack; - - /** Initialize your data structure here. */ - public MyQueue() - { - firstStack = new Stack(); - secondStack = new Stack(); - - } - - //Time Complexity - O(1) - //Space Complexity - O(1) - /** Push element x to the back of queue. */ - public void Push(int x) - { - firstStack.Push(x); - } - - //Time Complexity - O(n) - As part of peek, we are shifting elements from first stack to second stack - //Space Complexity - O(2n) which boils down to O(n) - As we are utilizing two stacks as part of peek - /** Removes the element from in front of queue and returns that element. */ - public int Pop() - { - Peek(); // This allows the flipping of order to FIFO when we move elements from first stack to second stack - return secondStack.Pop(); - } - - //Time Complexity - O(n) - As part of peek, we are shifting elements from first stack to second stack - //Space Complexity - O(2n) which boils down to O(n) - As we are utilizing two stacks as part of peek - /** Get the front element. */ - public int Peek() - { - // Transfer elements of first stack to second stack to flip the order - if (secondStack.Count == 0) - { - while (firstStack.Count != 0) - { - secondStack.Push(firstStack.Pop()); - } - } - - return secondStack.Peek(); - } - - //Time Complexity - O(1) - //Space Complexity - O(1) - /** Returns whether the queue is empty. */ - public bool Empty() - { - return firstStack.Count == 0 && secondStack.Count == 0; - } - } - - /** - * Your MyQueue object will be instantiated and called as such: - * MyQueue obj = new MyQueue(); - * obj.Push(x); - * int param_2 = obj.Pop(); - * int param_3 = obj.Peek(); - * bool param_4 = obj.Empty(); - */ -}