From 87aacfa3ce3db064b1c37140e5cb683356438c5a Mon Sep 17 00:00:00 2001 From: akanksha-vishwak Date: Fri, 14 Nov 2025 17:51:24 -0500 Subject: [PATCH] solution to both problems --- 01-Implement-Queue-Using-Stacks.py | 42 +++++++++++++++++ 02-Design-Hashmap.py | 73 ++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 01-Implement-Queue-Using-Stacks.py create mode 100644 02-Design-Hashmap.py diff --git a/01-Implement-Queue-Using-Stacks.py b/01-Implement-Queue-Using-Stacks.py new file mode 100644 index 00000000..9a91ce37 --- /dev/null +++ b/01-Implement-Queue-Using-Stacks.py @@ -0,0 +1,42 @@ +# Problem 1: Design Queue using Stacks (https://leetcode.com/problems/implement-queue-using-stacks/) + +# Time Complexity : O(1) for push operation, O(n) for pop and peek operations in the worst case +# Space Complexity : O(n) where n is the number of elements in the queue +# 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 +# We will use two stacks to implement the queue. The inStack will be used for enqueue operations (push) +# and the outStack will be used for dequeue operations (pop and peek). When outStack is empty, we will transfer +# all elements from inStack to outStack to maintain the correct order. + +class MyQueue: + + def __init__(self): + self.inStack = [] + self.outStack = [] + + def push(self, x: int) -> None: + self.inStack.append(x) + + def pop(self) -> int: + self.peek() + return self.outStack.pop() + + def peek(self) -> int: + if len(self.outStack) == 0: + while len(self.inStack) >0: + self.outStack.append(self.inStack.pop()) + return self.outStack[-1] + + def empty(self) -> bool: + return len(self.inStack) == 0 and len(self.outStack) == 0 + + + +# 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 diff --git a/02-Design-Hashmap.py b/02-Design-Hashmap.py new file mode 100644 index 00000000..83a7cfd5 --- /dev/null +++ b/02-Design-Hashmap.py @@ -0,0 +1,73 @@ +# Problem 2: Design HashMap (https://leetcode.com/problems/design-hashmap/) + +# Time Complexity : O(1) for put, get, and remove operations +# Space Complexity : O(n) where n is the number of unique keys added to the HashMap +# 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 +# We will use an array of linked lists to implement the HashMap. Each index in the array will represent a bucket. +# A hash function will be used to determine the index for a given key. Each bucket will be a linked list to handle collisions. +# We will also use a dummy head node for each linked list to simplify insertion and deletion operations. + +class MyHashMap: + class Node: + def __init__(self, key = None, value = None, next = None): + self.key = key + self.value = value + self.next = None + + def __init__(self): + self.arr = [None]*10000 + + def find(self, head, key): + prev = None + curr = head + + while curr!= None and curr.key != key: + prev = curr + curr = curr.next + return prev + + def hash_func(self, key): + return key%10000 + + def put(self, key: int, value: int) -> None: + idx = self.hash_func(key) + if self.arr[idx] == None: + self.arr[idx] = self.Node(-1, -1, None) #dummy + + # if key exists update the value, if not then create the value + prev = self.find(self.arr[idx], key) + if prev.next == None: #we were not able to find key + prev.next = self.Node(key, value, None) # creating new node + else: + prev.next.value = value #updating + + def get(self, key: int) -> int: + idx = self.hash_func(key) + if self.arr[idx] == None: # no LL initiated + return -1 + else: + prev = self.find(self.arr[idx], key) + if prev.next != None: #Node we are trying to find exists + return prev.next.value + else: + return -1 #key does not exist in LL + + def remove(self, key: int) -> None: + idx = self.hash_func(key) + if self.arr[idx] == None: # no LL initiated + return + else: + prev = self.find(self.arr[idx], key) + if prev.next == None: + return + else: + prev.next = prev.next.next + +# Your MyHashMap object will be instantiated and called as such: +# obj = MyHashMap() +# obj.put(key,value) +# param_2 = obj.get(key) +# obj.remove(key) \ No newline at end of file