Skip to content

Commit e5f2fda

Browse files
committed
Adding solutions for Tasks Before Session 3
1 parent 7d3e88a commit e5f2fda

File tree

48 files changed

+1370
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1370
-0
lines changed

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,25 @@ And here's a GIF of my InterviewBit Hashing and Linked Lists topic pages:
9898
* [GIF of my InterviewBit Hashing and Linked Lists topic pages](https://github.com/tachyonlabs/CodePath-Alumni-Professional-Interview-Prep-Course/blob/master/interviewbit-hashing-and-linked-lists-topics.gif)
9999

100100
When I went to take the HackerRank "CodePath Interview Prep - Unit 2 Test", once again I got a message about how I had already taken this test last summer and "can't login to a test that you have already completed" -- see the "My situation" description at the beginning of this README for an explanation of that -- so as directed by Patricia in Slack, I took the test as [email protected] instead of [email protected], and you should have the results.
101+
102+
## Tasks Before Session 3
103+
104+
As shown in this GIF, I've already completed most of the Trees problems, all but two of the Binary Search problems, and all but one of the Heaps and Maps problems, on InterviewBit (mostly in Python, but I've started doing Java versions of some of them as well):
105+
106+
* GIF of my InterviewBit Trees, Binary Search, and Heaps and Maps topic pages
107+
108+
#### My InterviewBit Trees solutions so far:
109+
110+
* Java: Inorder Traversal (recursion version), Inorder Traversal (stack version), Preorder Traversal (recursion version), Preorder Traversal (stack version), Max Depth of Binary Tree, and Min Depth of Binary Tree.
111+
112+
* Python: Next Greater Number BST, Valid Binary Search Tree, Vertical Order traversal of Binary Tree, Inorder Traversal, Postorder Traversal, Preorder Traversal, Identical Binary Trees, Symmetric Binary Tree, Sorted Array To Balanced BST, Binary Tree From Inorder And Postorder, Construct Binary Tree From Inorder And Preorder, Kth Smallest Element In Tree, Invert the Binary Tree, ZigZag Level Order Traversal BT, Populate Next Right Pointers Tree, Path Sum, Root to Leaf Paths With Sum, Max Depth of Binary Tree, Min Depth of Binary Tree, Sum Root to Leaf Numbers, and Flatten Binary Tree to Linked List.
113+
114+
#### My InterviewBit Binary Search solutions so far:
115+
116+
* Java: Square Root of Integer, Allocate Books, and Sorted Insert Position.
117+
118+
* Python: Count Element Occurence, Rotated Array, Square Root of Integer, Painter's Partition Problem, Allocate Books, Matrix Search, Search for a Range, Sorted Insert Position, Implement Power Function, and Rotated Sorted Array Search.
119+
120+
#### My InterviewBit Heaps and Maps solutions so far:
121+
122+
* Python: N max pair combinations, Magician and Chocolates, Merge K Sorted Lists, Distinct Numbers in Window, and LRU Cache.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class Solution {
2+
public int books(ArrayList<Integer> a, int b) {
3+
if (a.size() < b) {
4+
return -1;
5+
}
6+
int longestBook = Collections.max(a);
7+
if (a.size() == b) {
8+
return longestBook;
9+
}
10+
int low = longestBook;
11+
int high = a.stream().reduce(0, (x, y) -> x + y);
12+
int mid, student, sumPer;
13+
while (low <= high) {
14+
mid = (low + high) / 2;
15+
student = 1;
16+
sumPer = 0;
17+
for (int book : a) {
18+
if (sumPer + book > mid) {
19+
student++;
20+
sumPer = book;
21+
} else {
22+
sumPer += book;
23+
}
24+
}
25+
if (student <= b) {
26+
high = mid - 1;
27+
} else {
28+
low = mid + 1;
29+
}
30+
}
31+
return low;
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
# @param A : list of integers
3+
# @param B : integer
4+
# @return an integer
5+
def books(self, A, B):
6+
if len(A) < B:
7+
return -1
8+
if len(A) == B:
9+
return max(A)
10+
11+
low = max(A)
12+
high = sum(A)
13+
while low <= high:
14+
mid = (low + high) / 2
15+
students = 1
16+
sump = 0
17+
for book in A:
18+
if sump + book > mid:
19+
students += 1
20+
sump = book
21+
else:
22+
sump += book
23+
24+
if students <= B:
25+
high = mid - 1
26+
else:
27+
low = mid + 1
28+
29+
return low
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
# @param A : tuple of integers
3+
# @param B : integer
4+
# @return an integer
5+
def findCount(self, A, B):
6+
start = 0
7+
end = len(A) - 1
8+
while start <= end:
9+
mid = (start + end) / 2
10+
if A[mid] == B:
11+
count = 1
12+
i = mid - 1
13+
while i >= 0 and A[i] == B:
14+
count += 1
15+
i -= 1
16+
i = mid + 1
17+
while i < len(A) and A[i] == B:
18+
count += 1
19+
i += 1
20+
return count
21+
elif A[mid] < B:
22+
start = mid + 1
23+
elif A[mid] > B:
24+
end = mid - 1
25+
26+
return 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
# @param x : integer
3+
# @param n : integer
4+
# @param d : integer
5+
# @return an integer
6+
def pow(self, x, n, d):
7+
if x == 0 or d == 1:
8+
return 0
9+
10+
if n == 0:
11+
return 1
12+
elif n % 2:
13+
return ((x % d) * self.pow(x, n - 1, d)) % d
14+
else:
15+
y = self.pow(x, n / 2, d)
16+
return y ** 2 % d
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
# @param A : list of list of integers
3+
# @param B : integer
4+
# @return an integer
5+
def searchMatrix(self, A, B):
6+
rows = len(A)
7+
cols = len(A[0])
8+
start = 0
9+
end = rows * cols - 1
10+
while start <= end:
11+
mid = (start + end) / 2
12+
num = A[mid / rows][mid % rows]
13+
if num == B:
14+
return 1
15+
elif num < B:
16+
start = mid + 1
17+
elif num > b:
18+
end = mid - 1
19+
20+
return 0
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
# @param A : integer
3+
# @param B : integer
4+
# @param C : list of integers
5+
# @return an integer
6+
def paint(self, A, B, C):
7+
if not C:
8+
return 0
9+
10+
low, high = max(C) * B, sum(C) * B
11+
12+
if len(C) <= A:
13+
return low % 10000003
14+
15+
if A == 1:
16+
return high % 10000003
17+
18+
while low <= high:
19+
med = (low + high) / 2
20+
painter = 1
21+
boards = 0
22+
for board in C:
23+
if boards + board * B <= med:
24+
boards += board * B
25+
else:
26+
painter += 1
27+
boards = board * B
28+
29+
if painter <= A:
30+
high = med - 1
31+
else:
32+
low = med + 1
33+
34+
return low % 10000003
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
# @param A : tuple of integers
3+
# @return an integer
4+
def findMin(self, A):
5+
if len(A) == 1:
6+
return A[0]
7+
if A[-1] > A[0]:
8+
return A[0]
9+
10+
start = 0
11+
end = len(A) - 2
12+
while start <= end:
13+
mid = (start + end) / 2
14+
print A[mid:mid + 2]
15+
if A[mid] > A[mid + 1]:
16+
return A[mid + 1]
17+
elif A[mid] > A[start]:
18+
start = mid + 1
19+
else:
20+
end = mid
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution:
2+
# @param A : tuple of integers
3+
# @param B : integer
4+
# @return an integer
5+
def search(self, A, B):
6+
def find_pivot():
7+
low, high = 1, len(A) - 1
8+
while low <= high:
9+
mid = (low + high) / 2
10+
if A[mid - 1] > A[mid]:
11+
return mid
12+
if A[mid] > A[0]:
13+
low = mid + 1
14+
else:
15+
high = mid - 1
16+
17+
if not A:
18+
return -1
19+
20+
if len(A) == 1:
21+
return 0 if A[0] == B else -1
22+
23+
low, high = 0, len(A) - 1
24+
if A[0] > A[-1]:
25+
pivot = find_pivot()
26+
if B >= A[0]:
27+
low, high = 0, pivot - 1
28+
else:
29+
low, high = pivot, len(A) - 1
30+
31+
while low <= high:
32+
mid = (low + high) / 2
33+
if A[mid] == B:
34+
return mid
35+
if A[mid] > B:
36+
high = mid - 1
37+
else:
38+
low = mid + 1
39+
40+
return -1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
# @param A : tuple of integers
3+
# @param B : integer
4+
# @return a list of integers
5+
def searchRange(self, A, B):
6+
def find_border(side, start, end):
7+
while start <= end:
8+
mid = (start + end) / 2
9+
if (side == left and A[mid] == B and (mid == 0 or A[mid - 1] < B)) or (side == right and A[mid] == B and (mid == len(A) - 1 or A[mid + 1] > B)):
10+
return mid
11+
elif side == left and A[mid] >= B:
12+
end = mid - 1
13+
elif side == left and A[mid] < B:
14+
start = mid + 1
15+
elif side == right and A[mid] > B:
16+
end = mid - 1
17+
elif side == right and A[mid] <= B:
18+
start = mid + 1
19+
20+
return -1
21+
22+
23+
left = True
24+
right = False
25+
left_border = find_border(left, 0, len(A) - 1)
26+
if left_border == -1:
27+
return [-1, -1]
28+
29+
right_border = find_border(right, left_border, len(A) - 1)
30+
31+
return [left_border, right_border]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Solution {
2+
public int searchInsert(ArrayList<Integer> a, int b) {
3+
int start = 0;
4+
int end = a.size() - 1;
5+
int mid;
6+
int val;
7+
while (start <= end) {
8+
mid = (start + end) / 2;
9+
val = a.get(mid);
10+
if (val == b) {
11+
return mid;
12+
} else if (val < b) {
13+
start++;
14+
} else {
15+
end--;
16+
}
17+
}
18+
return start;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
# @param A : list of integers
3+
# @param B : integer
4+
# @return an integer
5+
def searchInsert(self, A, B):
6+
start = 0
7+
end = len(A) - 1
8+
while start <= end:
9+
mid = (start + end) / 2
10+
if A[mid] == B:
11+
return mid
12+
elif A[mid] < B:
13+
start = mid + 1
14+
else:
15+
end = mid - 1
16+
17+
return start
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Solution {
2+
public int sqrt(int a) {
3+
int start = 0;
4+
int end = a;
5+
int mid;
6+
long squared;
7+
while (start <= end) {
8+
mid = (start + end) / 2;
9+
squared = mid * mid;
10+
if (squared == a) {
11+
return mid;
12+
} else if (squared < a) {
13+
start = mid + 1;
14+
} else {
15+
end = mid - 1;
16+
}
17+
}
18+
return start - 1;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
# @param A : integer
3+
# @return an integer
4+
def sqrt(self, A):
5+
start = 0
6+
end = A
7+
while start <= end:
8+
mid = (start + end) / 2
9+
squared = mid ** 2
10+
if squared == A:
11+
return mid
12+
elif squared < A:
13+
start = mid + 1
14+
elif squared > A:
15+
end = mid - 1
16+
17+
return start - 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
# @param A : list of integers
3+
# @param B : integer
4+
# @return a list of integers
5+
def dNums(self, A, B):
6+
if B > len(A):
7+
return []
8+
9+
window = {}
10+
for num in A[:B]:
11+
window[num] = window.get(num, 0) + 1
12+
13+
distinct = [len(window)]
14+
for i in range(len(A) - B):
15+
remkey = A[i]
16+
if window[remkey] == 1:
17+
del window[remkey]
18+
else:
19+
window[remkey] -= 1
20+
21+
addkey = A[i + B]
22+
window[addkey] = window.get(addkey, 0) + 1
23+
distinct.append(len(window))
24+
25+
return distinct

0 commit comments

Comments
 (0)