From a0d923c03611dfdaaf81a889342a1608f54244ae Mon Sep 17 00:00:00 2001 From: Aduri Pavan Kaushik Date: Thu, 30 Jul 2026 19:20:44 -0700 Subject: [PATCH 1/2] design hashmap in python --- design_hashmap.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 design_hashmap.py diff --git a/design_hashmap.py b/design_hashmap.py new file mode 100644 index 00000000..0c89bf71 --- /dev/null +++ b/design_hashmap.py @@ -0,0 +1,66 @@ +#Time Complexity : O(1) +# Space Complexity : 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 + +class ListNode: + def __init__(self, key=-1, val=-1, next=None): + self.key = key + self.value = val + self.next = next + +class MyHashMap: + + def __init__(self): + self.primaryarray = 10000 + self.storage = [None] * self.primaryarray + + + def getKeyHash(self, key): + return key % self.primaryarray + + def getPrevNode(self, key, node): + prev = None + curr = node + while curr: + if curr.key == key: + return prev + prev = curr + curr = curr.next + return prev + + def put(self, key: int, value: int) -> None: + keyHash = self.getKeyHash(key) + if not self.storage[keyHash]: + self.storage[keyHash] = ListNode() + node = self.storage[keyHash] + prevNode = self.getPrevNode(key, node) + if not prevNode.next: + prevNode.next = ListNode(key, value) + else: + prevNode.next.value = value + + def get(self, key: int) -> int: + keyHash = self.getKeyHash(key) + if not self.storage[keyHash]: + return -1 + else: + node = self.storage[keyHash] + prev = self.getPrevNode(key, node) + if not prev.next: + return -1 + return prev.next.value + + + def remove(self, key: int) -> None: + keyHash = self.getKeyHash(key) + if not self.storage[keyHash]: + return + node = self.storage[keyHash] + prevNode = self.getPrevNode(key, node) + if not prevNode.next: + return + prevNode.next = prevNode.next.next \ No newline at end of file From 948994f003314aaff72b82efbc0a2ea68fb0a9d8 Mon Sep 17 00:00:00 2001 From: Aduri Pavan Kaushik Date: Thu, 30 Jul 2026 21:19:39 -0700 Subject: [PATCH 2/2] implementing queue using 2 stacks in python --- queue_using_two_stacks.py | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 queue_using_two_stacks.py diff --git a/queue_using_two_stacks.py b/queue_using_two_stacks.py new file mode 100644 index 00000000..c0df347f --- /dev/null +++ b/queue_using_two_stacks.py @@ -0,0 +1,50 @@ +#Time Complexity : Amortized O(1) for push, pop, and peek operations +# Space Complexity : 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 + + +class MyQueue: + + def __init__(self): + self.stack1 = [] + self.stack2 = [] + + + def push(self, x: int) -> None: + self.stack1.append(x) + + + def pop(self) -> int: + if self.stack2: + return self.stack2.pop() + else: + while self.stack1: + self.stack2.append(self.stack1.pop()) + return self.stack2.pop() + + + + + def peek(self) -> int: + if self.stack2: + return self.stack2[-1] + else: + while self.stack1: + self.stack2.append(self.stack1.pop()) + return self.stack2[-1] + return -1 + + + def empty(self) -> bool: + return not self.stack1 and not self.stack2 + +# Your MyQueue object will be instantiated and called as such: +# obj = MyQueue() +# obj.push(x) +# param_2 = obj.pop() +# param_3 = obj.peek() +# param_4 = obj.empty() \ No newline at end of file