From 3813bd4fda0c7bfbca5c3b785fbb2f5c7d3c8f72 Mon Sep 17 00:00:00 2001 From: Subba Paritala Date: Mon, 12 Jan 2026 13:06:12 -0600 Subject: [PATCH] Complete Design-2 in python --- design_hashmap.py | 84 +++++++++++++++++++++++++++++++++++++ design_queue_using_stack.py | 55 ++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 design_hashmap.py create mode 100644 design_queue_using_stack.py diff --git a/design_hashmap.py b/design_hashmap.py new file mode 100644 index 00000000..bdb8f03d --- /dev/null +++ b/design_hashmap.py @@ -0,0 +1,84 @@ +""" +Implemented hashmap using linkedList - used array to locate the address of the key using hash function +create dummy Node first and then point the given key, value to next +getprev method gets the previous Node so that we can handle all the cases easily +Time Complexity: O(1) for find the address and O(n) for finding the key +Space Complexity: O(n) +""" +class Node: + def __init__(self, key, val): + self.key = key + self.val = val + self.next = None + + +class MyHashMap: + + def __init__(self): + self.bucket = [None] * 1000 + + def hash1(self, key): + return key % 1000 + + def getPrev(self, key, head): + + prev = None + curr = head + + while curr and curr.key != key: + prev = curr + curr = curr.next + + return prev + + def put(self, key: int, value: int) -> None: + + h1 = self.hash1(key) + + if not self.bucket[h1]: + self.bucket[h1] = Node(-1, -1) + self.bucket[h1].next = Node(key, value) + return + + prev = self.getPrev(key, self.bucket[h1]) + + if prev.next: + prev.next.val = value + return + + prev.next = Node(key, value) + return + + def get(self, key: int) -> int: + + h1 = self.hash1(key) + + if not self.bucket[h1]: + return -1 + + prev = self.getPrev(key, self.bucket[h1]) + + if prev.next: + return prev.next.val + + return -1 + + def remove(self, key: int) -> None: + + h1 = self.hash1(key) + + if not self.bucket[h1]: + return + + prev = self.getPrev(key, self.bucket[h1]) + + if prev.next: + prev.next = prev.next.next + return + + +# Your MyHashMap object will be instantiated and called as such: +# obj = MyHashMap() +# obj.put(key,value) +# param_2 = obj.get(key) +# obj.remove(key) diff --git a/design_queue_using_stack.py b/design_queue_using_stack.py new file mode 100644 index 00000000..28b5df96 --- /dev/null +++ b/design_queue_using_stack.py @@ -0,0 +1,55 @@ +""" +Que is FIFO to implement this using stack means we can use two stacks +when value is pushed it goes first stack +when pop happens and the second stack is empty +we pop all elements from the first stack to second stack so that everything will revered so we can achieve FIFO type operations +if second stack is not empty we simply pop from there + +same applies to peek but we return the last element in the second stack +Time Complexity : O(1) +Space : O(n) +""" + +class MyQueue: + + def __init__(self): + self.in_stack = [] + self.out_stack = [] + + def push(self, x: int) -> None: + self.in_stack.append(x) + + def pop(self) -> int: + + if self.out_stack: + return self.out_stack.pop() + + while self.in_stack: + self.out_stack.append(self.in_stack.pop()) + + return self.out_stack.pop() + + def peek(self) -> int: + + if self.out_stack: + return self.out_stack[-1] + + while self.in_stack: + self.out_stack.append(self.in_stack.pop()) + + print(self.out_stack) + return self.out_stack[-1] + + def empty(self) -> bool: + + if not self.in_stack and not self.out_stack: + return True + return False + + +# 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()