Skip to content

Instantly share code, notes, and snippets.

View abhinavjonnada82's full-sized avatar

ABHINAV JONNADA abhinavjonnada82

  • Washington, DC
View GitHub Profile
# Recursion
def fibonaci(n):
if n == 1 or n == 0:
return 1
else:
return fibonaci(n-1) + fibonaci(n-2)
def factorial(n):
if n == 1 or n == 0:
return 1
def maxDepth(self, root):
if root is None:
return 0
return 1 + max(self.maxDepth(root.left), maxDepth(root.right))
def levelOrderTraversal(root):
if root is None:
return 0
q = Queue()
q.enqueue(root)
# Container Most Water
def containerMostWater(arr):
if len(arr) < 2:
return -1
i = 0
j = len(arr)-1
curr = 0
maxVal = 0
while i < j:
curr = min(arr[i], arr[j]) * j-i
connections = [("l", "he"), ("h", "l"), ("d", "h"),
("he", "d"), ("t", "o"), ("o", "t"),
("p", None), ("l", "p"), ("he", "t"), ("h", "p")]
# l -> he, p
# h -> l
# d -> h
# he -> d
# t -> o
# o -> t
'''
i/p: [2,7,11,15], 9 i/p: [1,4,5,2,8], 9
o/p: [0,1] o/p: [1,8], [4,5]
-> Place 2 pointers one at i = 0 & j = len(index)-1
-> Compute target using if-condition
-> if target > increase i pointers
-> else decrease j pointers
-> once target is found append into a list
-> end it once i < j condition fails
'''
i/p: [2,2,1]
o/p: 1
i/p: [4,1,2,1,2]
o/p: 4
i/p: [4,4,4,4]
o/p: None
'''
i/P: lisA = [4,1,8,4,5] lisB = [5,0,1,8,4,5]
o/p: 8
i/p: lisA = [0,9,1,2,4] lisB = [3,2,4]
o/p: 2
i/p: lisA = [3,5,9,3,4] lisB = [7,9,3,4]
o/p: 9
1.) What does a hashmap do? What are the methods that the client will use?
Hash Map are data structures that store key-value pairs. They perform get, put and contains operations all in O(1) runtime in the average case..
2.) Describe how a hashmap works concisely. Use less than 150 characters.
With a hash map, we store all of the keys and values in an array. To find the array index that a given key-value pair is stored, we will rely on
a hash function to compte this array index
3.) What is a hash function?
A hash function is just a function that takes in a key and returns an integer value that is in the range [0,1,2,...., M-1].
class ListNode:
def __init__(self, val):
self.val = val
self.next = None
self.prev = None
# head <-> tail
# head <-> 3 <-> 1 <-> 2 <-> tail
#head: prev: None
class Node:
def __init__(self, val, key):
self.prev = None
self.next = None
self.val = val
self.key = key
class DoublyLinkedList:
def __init__(self):
self.back = Node("back", "back")