This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |