Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions design_hashmap.py
Original file line number Diff line number Diff line change
@@ -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)
55 changes: 55 additions & 0 deletions design_queue_using_stack.py
Original file line number Diff line number Diff line change
@@ -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()