From 5c0ba298be567c2664cbc6abd7df8fff617f7ecb Mon Sep 17 00:00:00 2001 From: mohithchandranarra Date: Sat, 5 Sep 2026 22:01:40 -0400 Subject: [PATCH 1/4] Solution - Implement Queue using only 2 stacks --- Sample.java | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/Sample.java b/Sample.java index 1739a9cb..a6bebb5f 100644 --- a/Sample.java +++ b/Sample.java @@ -5,3 +5,47 @@ // Your code here along with comments explaining your approach +class MyQueue { + private Stack inStack; + private Stack outStack; + + public MyQueue() { + this.inStack = new Stack<>(); + this.outStack = new Stack<>(); + } + + public void push(int x) { + inStack.push(x); + } + + public int pop() { + if(outStack.isEmpty()){ + while(!inStack.isEmpty()){ + outStack.push(inStack.pop()); + } + } + return outStack.pop(); + } + + public int peek() { + if(outStack.isEmpty()){ + while(!inStack.isEmpty()){ + outStack.push(inStack.pop()); + } + } + return outStack.peek(); + } + + public boolean empty() { + return inStack.isEmpty() && outStack.isEmpty(); + } +} + +/** + * 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 From 3c9dc53e4818ab6ec535efb652cb57d4a26460a7 Mon Sep 17 00:00:00 2001 From: mohithchandranarra Date: Sat, 5 Sep 2026 22:07:04 -0400 Subject: [PATCH 2/4] Added Time Complexities --- Sample.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sample.java b/Sample.java index a6bebb5f..98b2f357 100644 --- a/Sample.java +++ b/Sample.java @@ -14,10 +14,12 @@ public MyQueue() { this.outStack = new Stack<>(); } + //Time Complexity - O(1) public void push(int x) { inStack.push(x); } + //Time Complexity - O(1) public int pop() { if(outStack.isEmpty()){ while(!inStack.isEmpty()){ @@ -27,6 +29,7 @@ public int pop() { return outStack.pop(); } + //Time Complexity - O(1) public int peek() { if(outStack.isEmpty()){ while(!inStack.isEmpty()){ @@ -36,6 +39,7 @@ public int peek() { return outStack.peek(); } + //Time Complexity - O(1) public boolean empty() { return inStack.isEmpty() && outStack.isEmpty(); } From e5f8e04ca31549bb6a068bde17c6b6d77e83e342 Mon Sep 17 00:00:00 2001 From: mohithchandranarra Date: Sat, 5 Sep 2026 22:12:35 -0400 Subject: [PATCH 3/4] Added Comments --- Sample.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Sample.java b/Sample.java index 98b2f357..8b1bc570 100644 --- a/Sample.java +++ b/Sample.java @@ -1,7 +1,8 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : +// Time Complexity : Amortized time complexity for operations - push, pop, peek , Empty is O(1). +// Time Complexity : Worst case time complexity for operations - pop and peek is O(n) when the outStack is Empty. +// Space Complexity : 2 * O(n) = O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No // Your code here along with comments explaining your approach From eefbc85ebe9260225c0682cd6c29d6caded9c19a Mon Sep 17 00:00:00 2001 From: mohithchandranarra Date: Sun, 6 Sep 2026 00:04:19 -0400 Subject: [PATCH 4/4] Design HashMap - Solution --- MyHashMap.java | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 MyHashMap.java diff --git a/MyHashMap.java b/MyHashMap.java new file mode 100644 index 00000000..cd9c141e --- /dev/null +++ b/MyHashMap.java @@ -0,0 +1,81 @@ +// Time Complexity : Amortized time complexity for operations - put, get, remove is O(1). +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +class MyHashMap { + class Node{ + int key; + int value; + Node next; + public Node(int key, int value){ + this.key = key; + this.value = value; + } + } + private Node [] storage; + private int hash(int key){ + return key % 10000; + } + + public MyHashMap() { + this.storage = new Node[10000]; + } + + private Node find(Node head, int key){ + Node previous = head; + Node current = head.next; + while(current!=null && current.key != key){ + previous = current; + current = current.next; + } + return previous; + } + + public void put(int key, int value) { + int itemIndex = hash(key); + if(storage[itemIndex] == null){ + storage[itemIndex] = new Node(-1,-1); + } + Node previous = find(storage[itemIndex], key); + if(previous.next == null){ + previous.next = new Node(key, value); + } else{ + previous.next.value = value; + } + } + + public int get(int key) { + int itemIndex = hash(key); + if(storage[itemIndex] == null){ + return -1; + } + Node previous = find(storage[itemIndex], key); + if(previous.next == null){ + return -1; + } + return previous.next.value; + } + + public void remove(int key) { + int itemIndex = hash(key); + if(storage[itemIndex] == null){ + return; + } + Node previous = find(storage[itemIndex], key); + if(previous.next == null){ + return; + } + Node temp = previous.next; + previous.next = temp.next; + temp.next = null; + } +} + +/** + * 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