From 4650328fcc14098f3c5bde8672fa96c4ff503fe2 Mon Sep 17 00:00:00 2001 From: Harshitha Thondalapally Date: Thu, 25 Dec 2025 18:28:45 -0500 Subject: [PATCH 1/3] Completed Design 2 --- src/MyHashMap.java | 69 ++++++++++++++++++++++++++++++++++++++++++ src/MyQueue.java | 36 ++++++++++++++++++++++ src/TestHomework2.java | 18 +++++++++++ 3 files changed, 123 insertions(+) create mode 100644 src/MyHashMap.java create mode 100644 src/MyQueue.java create mode 100644 src/TestHomework2.java diff --git a/src/MyHashMap.java b/src/MyHashMap.java new file mode 100644 index 00000000..f8bd484e --- /dev/null +++ b/src/MyHashMap.java @@ -0,0 +1,69 @@ +class MyHashMap { + private class Node{ + int key; + int value; + Node next; + public Node(int key, int value){ + this.key =key; + this.value = value; + } + } + private static final int DEFAULT_CAPACITY = 1000; + private Node [] buckets; + + public MyHashMap() { + buckets = new Node[DEFAULT_CAPACITY]; + } + private int hash(int key){ + return key % DEFAULT_CAPACITY; + } + + public void put(int key, int value) { + int hash = hash(key); + Node cur = buckets[hash]; + if(buckets[hash] == null){ + buckets[hash] = new Node(key, value); + return; + } + while(true){ + if(cur.key == key){ + cur.value = value; + return; + } + if(cur.next == null) break; + cur = cur.next; + } + cur.next = new Node(key, value); + } + + public int get(int key) { + int hash = hash(key); + Node cur = buckets[hash]; + if(buckets[hash] == null){ + return -1; + } + while(cur != null){ + if(cur.key == key){ + return cur.value; + } + cur = cur.next; + } + return -1; + } + + public void remove(int key) { + int hash = hash(key); + Node cur = buckets[hash]; + while (cur == null) return; + if(cur.key == key){ + buckets[hash] = cur.next; + return; + } + while(cur.next != null){ + if(cur.key == key){ + cur.next = cur.next.next; + return; + } + } + } +} \ No newline at end of file diff --git a/src/MyQueue.java b/src/MyQueue.java new file mode 100644 index 00000000..947cbcda --- /dev/null +++ b/src/MyQueue.java @@ -0,0 +1,36 @@ +import java.util.Stack; + +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() { + peek(); + 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(); + } +} diff --git a/src/TestHomework2.java b/src/TestHomework2.java new file mode 100644 index 00000000..11bf8db8 --- /dev/null +++ b/src/TestHomework2.java @@ -0,0 +1,18 @@ + +void main() { + MyHashMap myHashMap = new MyHashMap(); + myHashMap.put(1,1); + myHashMap.put(2,2); + System.out.println(myHashMap.get(1)); + System.out.println(myHashMap.get(3)); + myHashMap.put(2,1); + System.out.println(myHashMap.get(2)); + myHashMap.remove(2); + System.out.println(myHashMap.get(2)); + MyQueue myQueue = new MyQueue(); + myQueue.push(1); + myQueue.push(2); + System.out.println(myQueue.peek()); + System.out.println(myQueue.pop()); + myQueue.empty(); +} \ No newline at end of file From b9fe3d13c1aff5fd759741273cf02a1a40c4ca58 Mon Sep 17 00:00:00 2001 From: Harshitha Thondalapally Date: Thu, 25 Dec 2025 18:34:57 -0500 Subject: [PATCH 2/3] Added problem analysis --- src/MyHashMap.java | 17 +++++++++++++++++ src/MyQueue.java | 6 +++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/MyHashMap.java b/src/MyHashMap.java index f8bd484e..289fc793 100644 --- a/src/MyHashMap.java +++ b/src/MyHashMap.java @@ -1,3 +1,20 @@ +/* +I use an array of buckets where each bucket stores a linked list to handle hash collisions using separate chaining. +The key is mapped to a bucket index using a hash function (key % size), and all operations traverse only that bucket. +This gives average O(1) time for add, remove, and contains, with O(n) worst case if many keys collide. +Time Complexity (average): + add: O(1) + remove: O(1) + contains: O(1) +Worst case (many collisions in one bucket): + O(n) + +Space Complexity: + O(n) + // Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No +*/ + class MyHashMap { private class Node{ int key; diff --git a/src/MyQueue.java b/src/MyQueue.java index 947cbcda..821195ab 100644 --- a/src/MyQueue.java +++ b/src/MyQueue.java @@ -1,5 +1,9 @@ import java.util.Stack; - +/* +I use two stacks: an input stack for push operations and an output stack for pop/peek operations. +When output is empty, and we need to pop/peek, I move all elements from input to output which reverses the order and simulates FIFO. +Each element moves from input to output at most once, so operations are amortized O(1). +*/ class MyQueue { private Stack inStack; private Stack outStack; From f711006969ea704bf3f117307524b2425ea96caa Mon Sep 17 00:00:00 2001 From: Harshitha Thondalapally Date: Mon, 29 Dec 2025 13:15:58 -0500 Subject: [PATCH 3/3] addressed comments --- src/MyHashMap.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/MyHashMap.java b/src/MyHashMap.java index 289fc793..f7de90e2 100644 --- a/src/MyHashMap.java +++ b/src/MyHashMap.java @@ -71,16 +71,18 @@ public int get(int key) { public void remove(int key) { int hash = hash(key); Node cur = buckets[hash]; - while (cur == null) return; + if (cur == null) return; + // remove head if(cur.key == key){ buckets[hash] = cur.next; return; } while(cur.next != null){ - if(cur.key == key){ + if(cur.next.key == key){ cur.next = cur.next.next; return; } + cur = cur.next; } } } \ No newline at end of file