Skip to content

Commit b7de4cb

Browse files
committed
Added assessment questions + solutions
1 parent 2264802 commit b7de4cb

32 files changed

+602
-1
lines changed

.vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"python.linting.enabled": false
2+
"python.linting.enabled": false,
3+
"python.languageServer": "None"
34
}

Assessments/balance-index.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
# BALANCE INDEX
3+
4+
# O(N) time and O(1) space, where N -> length of input array
5+
def balanceIndex(array):
6+
# Write your code here.
7+
rightSum = sum(array)
8+
leftSum = 0
9+
10+
for i in range(len(array)):
11+
leftSum = leftSum + array[i - 1] if i != 0 else leftSum
12+
rightSum -= array[i]
13+
if leftSum == rightSum:
14+
return i
15+
16+
return -1

Assessments/build-failures.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
# BUILD FAILURES
3+
4+
# O(N*log(S)) time, O(1) space, where N -> length of buildRuns array
5+
# S -> length of longest buildRun
6+
def buildFailures(buildRuns):
7+
# Write your code here.
8+
longestRun = 0
9+
currentRun = 1
10+
currentHighest = float("-inf")
11+
12+
for buildRun in buildRuns:
13+
start = 0
14+
end = len(buildRun) - 1
15+
mid = 0
16+
while start <= end:
17+
mid = (start + end) // 2
18+
if buildRun[mid] == False:
19+
if buildRun[mid - 1] == True:
20+
break
21+
else:
22+
end = mid - 1
23+
24+
else:
25+
start = mid + 1
26+
27+
totalBuilds = len(buildRun)
28+
percentage = mid / totalBuilds * 100
29+
30+
if percentage >= currentHighest:
31+
currentRun = 1
32+
else:
33+
currentRun += 1
34+
35+
currentHighest = percentage
36+
longestRun = max(longestRun, currentRun)
37+
38+
return longestRun if longestRun > 1 else -1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
# COUNT CONTAINED PERMUTATIONS
3+
4+
# O(M * U + N) time and O(U) space, where M -> length of big string,
5+
# U -> number of unique characters in small string, N -> length
6+
# of small string.
7+
# U is actually a constant since it can't be greater than 26. and
8+
# M > N, so M will dissolve N
9+
# So, modified complexities:
10+
# O(M) time and O(1) space, M -> length of big string
11+
def countContainedPermutations(bigString, smallString):
12+
# Write your code here.
13+
smallCount, bigCount = {}, {}
14+
for letter in smallString:
15+
if letter not in smallCount:
16+
smallCount[letter] = 0
17+
smallCount[letter] += 1
18+
19+
bigSize, smallSize = len(bigString), len(smallString)
20+
start, end, totalCount = 0, 0, 0
21+
22+
while end < bigSize:
23+
letterToAdd = bigString[end]
24+
if letterToAdd not in bigCount:
25+
bigCount[letterToAdd] = 0
26+
bigCount[letterToAdd] += 1
27+
28+
if end - start == smallSize:
29+
letterToRemove = bigString[start]
30+
if bigCount[letterToRemove] == 1:
31+
del bigCount[letterToRemove]
32+
else:
33+
bigCount[letterToRemove] -= 1
34+
35+
start += 1
36+
37+
if matchCounts(bigCount, smallCount):
38+
totalCount += 1
39+
40+
end += 1
41+
42+
return totalCount
43+
44+
def matchCounts(bigCount, smallCount):
45+
for letter in smallCount:
46+
if letter not in bigCount:
47+
return False
48+
if smallCount[letter] != bigCount[letter]:
49+
return False
50+
51+
return True

Assessments/degrees-of-separation.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
# DEGREES OF SEPARATION
3+
4+
# O(V + E) time, O(V) space
5+
def degreesOfSeparation(friendsLists, personOne, personTwo):
6+
# Write your code here.
7+
degrees, visited = {}, {}
8+
findDegrees(friendsLists, personOne, degrees, visited, 0)
9+
getUnconnectedPeople(friendsLists, degrees)
10+
countOne = 0
11+
for person in degrees:
12+
if degrees[person] > 6:
13+
countOne += 1
14+
15+
print(degrees)
16+
degrees, visited = {}, {}
17+
findDegrees(friendsLists, personTwo, degrees, visited, 0)
18+
getUnconnectedPeople(friendsLists, degrees)
19+
countTwo = 0
20+
for person in degrees:
21+
if degrees[person] > 6:
22+
countTwo += 1
23+
print(degrees)
24+
if countOne < countTwo:
25+
return personOne
26+
elif countTwo < countOne:
27+
return personTwo
28+
else:
29+
return ""
30+
31+
def findDegrees(friendsLists, person, degrees, visited, level):
32+
degrees[person] = 0
33+
queue = [(person, 0)]
34+
#visited = {person: True}
35+
while queue:
36+
current = queue.pop(0)
37+
#visited[current[0]] = True
38+
level = current[1]
39+
for friend in friendsLists[current[0]]:
40+
if friend in visited:
41+
continue
42+
visited[friend] = True
43+
degrees[friend] = level + 1
44+
queue.append((friend, level + 1))
45+
46+
47+
def getUnconnectedPeople(friendsLists, degrees):
48+
for friend in friendsLists:
49+
if friend not in degrees:
50+
degrees[friend] = float("inf")

Assessments/glob-matching.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
# GLOB MATCHING
3+
4+
# O(n * m) time and space
5+
def globMatching(fileName, pattern):
6+
# Write your code here.
7+
match = [[False for _ in range(len(pattern) + 1)] for _ in range(len(fileName) + 1)]
8+
match[0][0] = True
9+
for i in range(1, len(fileName) + 1):
10+
match[i][0] = False
11+
12+
for row in range(len(fileName) + 1):
13+
for col in range(1, len(pattern) + 1):
14+
if row == 0:
15+
if pattern[col - 1] == "*":
16+
match[row][col] = match[row][col - 1]
17+
continue
18+
19+
if pattern[col - 1] == "*":
20+
match[row][col] = match[row - 1][col - 1] or match[row][col - 1] or match[row - 1][col]
21+
elif pattern[col - 1] == "?":
22+
match[row][col] = match[row - 1][col - 1]
23+
else:
24+
match[row][col] = match[row - 1][col - 1] and fileName[row - 1] == pattern[col - 1]
25+
print(match)
26+
return match[-1][-1]
27+

Assessments/inverted-bisection.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
# INVERTED BISECTION
3+
4+
# This is an input class. Do not edit.
5+
class LinkedList:
6+
def __init__(self, value):
7+
self.value = value
8+
self.next = None
9+
10+
# O(N) time and O(1) space
11+
def invertedBisection(head):
12+
# Write your code here.
13+
if head is None or head.next is None:
14+
return head
15+
16+
prev = None
17+
slow = head
18+
fast = head
19+
while fast and fast.next:
20+
prev = slow
21+
slow = slow.next
22+
fast = fast.next.next
23+
24+
hasEvenCount = True if fast == None else False
25+
prev.next = None
26+
if hasEvenCount:
27+
firstHalf = reverseLinkedList(head)
28+
secondHalf = reverseLinkedList(slow)
29+
head.next = secondHalf
30+
return firstHalf
31+
else:
32+
firstHalf = reverseLinkedList(head)
33+
secondHalf = reverseLinkedList(slow.next)
34+
head.next = slow
35+
slow.next = secondHalf
36+
return firstHalf
37+
38+
def reverseLinkedList(head):
39+
prev = None
40+
cur = head
41+
while cur is not None:
42+
temp = cur.next
43+
cur.next = prev
44+
prev = cur
45+
cur = temp
46+
47+
return prev
48+

Assessments/largest-bst-size.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
# LARGEST BST SIZE
3+
4+
# O(n) time and O(h) space, n -> number of nodes, h -> height of tree
5+
def largestBstSize(tree):
6+
# Write your code here.
7+
totalNodes, largestSize, isBST = getLargest(tree)
8+
return largestSize
9+
10+
def getLargest(node):
11+
if node == None:
12+
return (0, 0, True)
13+
14+
left = node.left.value if node.left is not None else float("-inf")
15+
right = node.right.value if node.right is not None else float("inf")
16+
countLeft, largestLeft, isLeftBST = getLargest(node.left)
17+
countRight, largestRight, isRightBST = getLargest(node.right)
18+
19+
if left < node.value <= right and isLeftBST and isRightBST:
20+
totalCount = countLeft + countRight + 1
21+
return (totalCount, totalCount, True)
22+
23+
else:
24+
return (0, max(largestLeft, largestRight), False)
25+
26+
27+
# This is an input class. Do not edit.
28+
class BinaryTree:
29+
def __init__(self, value):
30+
self.value = value
31+
self.left = None
32+
self.right = None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
# LONGEST INCREASING MATRIX PATH
3+
4+
# O(M * N) time, O(M * N) space, M -> number of rows in input matrix
5+
# N -> number of columns in input matrix
6+
def longestIncreasingMatrixPath(matrix):
7+
# Write your code here.
8+
maxLength = 0
9+
m, n = len(matrix), len(matrix[0])
10+
cache = [[None for i in range(n)] for j in range(m)]
11+
12+
for i in range(m):
13+
for j in range(n):
14+
length = findLongest(matrix, i, j, cache)
15+
maxLength = max(maxLength, length)
16+
17+
return maxLength
18+
19+
def findLongest(matrix, row, col, cache):
20+
if cache[row][col] != None:
21+
return cache[row][col]
22+
23+
num = matrix[row][col]
24+
matrix[row][col] = None
25+
26+
neighbors = getNeighbors(matrix, row, col)
27+
maximum = float("-inf")
28+
for i, j in neighbors:
29+
if matrix[i][j] == None or matrix[i][j] <= num:
30+
continue
31+
getMaxLength = findLongest(matrix, i, j, cache)
32+
maximum = max(maximum, 1 + getMaxLength)
33+
34+
matrix[row][col] = num
35+
cache[row][col] = maximum if maximum != float("-inf") else 1
36+
37+
return cache[row][col]
38+
39+
def getNeighbors(matrix, row, col):
40+
neighbors = []
41+
42+
if row != 0:
43+
neighbors.append([row - 1, col])
44+
if row != len(matrix) - 1:
45+
neighbors.append([row + 1, col])
46+
if col != 0:
47+
neighbors.append([row, col - 1])
48+
if col != len(matrix[0]) - 1:
49+
neighbors.append([row, col + 1])
50+
51+
return neighbors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
# LONGEST STREAK OF ADJACENT ONES
3+
4+
# O(N) time and O(1) space
5+
def longestStreakOfAdjacentOnes(array):
6+
# Write your code here.
7+
index = -1
8+
maximumOnes = 0
9+
10+
for i in range(len(array)):
11+
if array[i] == 0:
12+
left = 0
13+
j = i - 1
14+
while j >= 0 and array[j] == 1:
15+
j -= 1
16+
left += 1
17+
18+
j = i + 1
19+
right = 0
20+
while j < len(array) and array[j] == 1:
21+
j += 1
22+
right += 1
23+
24+
total = left + right + 1
25+
if total > maximumOnes:
26+
maximumOnes = total
27+
index = i
28+
29+
return index
30+
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
# MAX SUBSEQUENCE DOT PRODUCT
3+
4+
# O(m * n) time, O(m * n) space; m is the length of arrayOne and
5+
# n is the length of arrayTwo
6+
def maxSubsequenceDotProduct(arrayOne, arrayTwo):
7+
# Write your code here.
8+
# This solution takes care of a situation where the max dot product is less than zero.
9+
# This covers all edges, so this is the correct optimal solution.
10+
11+
m, n = len(arrayOne), len(arrayTwo)
12+
dp = [[float("-inf") for i in range(m + 1)] for j in range(n + 1)]
13+
14+
for i in range(1, n + 1):
15+
for j in range(1, m + 1):
16+
first = arrayOne[j - 1]
17+
second = arrayTwo[i - 1]
18+
product = first * second
19+
sum = product if dp[i - 1][j - 1] == float("-inf") else dp[i - 1][j - 1] + product
20+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1], sum)
21+
22+
return dp[-1][-1]
23+

0 commit comments

Comments
 (0)