Skip to content

Commit e673440

Browse files
committed
Add solutions to Interview Preparation Kit
1 parent 8cc5b1f commit e673440

Some content is hidden

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

49 files changed

+1594
-0
lines changed

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Solutions to HackerRank problems
77
* [Linux Shell](#linux-shell)
88
* [CPP](#cpp)
99
* [C](#c)
10+
* [Interview Preparation Kit](#interview-preparation-kit)
1011

1112
## Algorithms
1213
| Subdomain | Difficulty | Challenge | Solution |
@@ -473,3 +474,56 @@ Solutions to HackerRank problems
473474
|Structs and Enums|Easy|[Boxes through a Tunnel](https://www.hackerrank.com/challenges/too-high-boxes/problem)|[too-high-boxes.c](c/too-high-boxes.c)|
474475
|Structs and Enums|Medium|[Small Triangles, Large Triangles](https://www.hackerrank.com/challenges/small-triangles-large-triangles/problem)|[small-triangles-large-triangles.c](c/small-triangles-large-triangles.c)|
475476
|Structs and Enums|Hard|[Post Transition](https://www.hackerrank.com/challenges/post-transition/problem)|[post-transition.c](c/post-transition.c)|
477+
478+
479+
## Interview Preparation Kit
480+
| Subdomain | Difficulty | Challenge | Solution |
481+
|:---------:|:----------:|:---------:|:--------:|
482+
|Warmup|Easy|[Counting Valleys](https://www.hackerrank.com/challenges/counting-valleys/problem)|[counting-valleys.py](interview-preparation-kit/counting-valleys.py)|
483+
|Warmup|Easy|[Jumping on the Clouds](https://www.hackerrank.com/challenges/jumping-on-the-clouds/problem)|[jumping-on-the-clouds.py](interview-preparation-kit/jumping-on-the-clouds.py)|
484+
|Warmup|Easy|[Repeated String](https://www.hackerrank.com/challenges/repeated-string/problem)|[repeated-string.py](interview-preparation-kit/repeated-string.py)|
485+
|Warmup|Easy|[Sock Merchant](https://www.hackerrank.com/challenges/sock-merchant/problem)|[sock-merchant.py](interview-preparation-kit/sock-merchant.py)|
486+
|Arrays|Easy|[2D Array - DS](https://www.hackerrank.com/challenges/2d-array/problem)|[2d-array.py](interview-preparation-kit/2d-array.py)|
487+
|Arrays|Easy|[Arrays: Left Rotation](https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem)|[ctci-array-left-rotation.py](interview-preparation-kit/ctci-array-left-rotation.py)|
488+
|Arrays|Medium|[Minimum Swaps 2](https://www.hackerrank.com/challenges/minimum-swaps-2/problem)|[minimum-swaps-2.py](interview-preparation-kit/minimum-swaps-2.py)|
489+
|Arrays|Medium|[New Year Chaos](https://www.hackerrank.com/challenges/new-year-chaos/problem)|[new-year-chaos.py](interview-preparation-kit/new-year-chaos.py)|
490+
|Arrays|Hard|[Array Manipulation](https://www.hackerrank.com/challenges/crush/problem)|[crush.py](interview-preparation-kit/crush.py)|
491+
|Dictionaries and Hashmaps|Easy|[Hash Tables: Ransom Note](https://www.hackerrank.com/challenges/ctci-ransom-note/problem)|[ctci-ransom-note.py](interview-preparation-kit/ctci-ransom-note.py)|
492+
|Dictionaries and Hashmaps|Easy|[Two Strings](https://www.hackerrank.com/challenges/two-strings/problem)|[two-strings.py](interview-preparation-kit/two-strings.py)|
493+
|Dictionaries and Hashmaps|Medium|[Count Triplets](https://www.hackerrank.com/challenges/count-triplets-1/problem)|[count-triplets-1.py](interview-preparation-kit/count-triplets-1.py)|
494+
|Dictionaries and Hashmaps|Medium|[Frequency Queries](https://www.hackerrank.com/challenges/frequency-queries/problem)|[frequency-queries.py](interview-preparation-kit/frequency-queries.py)|
495+
|Dictionaries and Hashmaps|Medium|[Sherlock and Anagrams](https://www.hackerrank.com/challenges/sherlock-and-anagrams/problem)|[sherlock-and-anagrams.py](interview-preparation-kit/sherlock-and-anagrams.py)|
496+
|Graphs|Hard|[BFS: Shortest Reach in a Graph](https://www.hackerrank.com/challenges/ctci-bfs-shortest-reach/problem)|[ctci-bfs-shortest-reach.py](interview-preparation-kit/ctci-bfs-shortest-reach.py)|
497+
|Graphs|Hard|[DFS: Connected Cell in a Grid](https://www.hackerrank.com/challenges/ctci-connected-cell-in-a-grid/problem)|[ctci-connected-cell-in-a-grid.py](interview-preparation-kit/ctci-connected-cell-in-a-grid.py)|
498+
|Greedy Algorithms|Easy|[Luck Balance](https://www.hackerrank.com/challenges/luck-balance/problem)|[luck-balance.py](interview-preparation-kit/luck-balance.py)|
499+
|Greedy Algorithms|Easy|[Minimum Absolute Difference in an Array](https://www.hackerrank.com/challenges/minimum-absolute-difference-in-an-array/problem)|[minimum-absolute-difference-in-an-array.py](interview-preparation-kit/minimum-absolute-difference-in-an-array.py)|
500+
|Greedy Algorithms|Medium|[Greedy Florist](https://www.hackerrank.com/challenges/greedy-florist/problem)|[greedy-florist.py](interview-preparation-kit/greedy-florist.py)|
501+
|Greedy Algorithms|Medium|[Max Min](https://www.hackerrank.com/challenges/angry-children/problem)|[angry-children.py](interview-preparation-kit/angry-children.py)|
502+
|linked Lists|Easy|[Find Merge Point of Two Lists](https://www.hackerrank.com/challenges/find-the-merge-point-of-two-joined-linked-lists/problem)|[find-the-merge-point-of-two-joined-linked-lists.py](interview-preparation-kit/find-the-merge-point-of-two-joined-linked-lists.py)|
503+
|Linked Lists|Easy|[Insert a node at a specific position in a linked list](https://www.hackerrank.com/challenges/insert-a-node-at-a-specific-position-in-a-linked-list/problem)|[insert-a-node-at-a-specific-position-in-a-linked-list.py](interview-preparation-kit/insert-a-node-at-a-specific-position-in-a-linked-list.py)|
504+
|Linked Lists|Easy|[Inserting a Node Into a Sorted Doubly Linked List](https://www.hackerrank.com/challenges/insert-a-node-into-a-sorted-doubly-linked-list/problem)|[insert-a-node-into-a-sorted-doubly-linked-list.py](interview-preparation-kit/insert-a-node-into-a-sorted-doubly-linked-list.py)|
505+
|Linked Lists|Easy|[Linked Lists: Detect a Cycle](https://www.hackerrank.com/challenges/ctci-linked-list-cycle/problem)|[ctci-linked-list-cycle.py](interview-preparation-kit/ctci-linked-list-cycle.py)|
506+
|Linked Lists|Easy|[Reverse a doubly linked list](https://www.hackerrank.com/challenges/reverse-a-doubly-linked-list/problem)|[reverse-a-doubly-linked-list.py](interview-preparation-kit/reverse-a-doubly-linked-list.py)|
507+
|Miscellaneous|Easy|[Flipping bits](https://www.hackerrank.com/challenges/flipping-bits/problem)|[flipping-bits.py](interview-preparation-kit/flipping-bits.py)|
508+
|Miscellaneous|Medium|[Time Complexity: Primality](https://www.hackerrank.com/challenges/ctci-big-o/problem)|[ctci-big-o.py](interview-preparation-kit/ctci-big-o.py)|
509+
|Recursion and Backtracking|Easy|[Recursion: Fibonacci Numbers](https://www.hackerrank.com/challenges/ctci-fibonacci-numbers/problem)|[ctci-fibonacci-numbers.py](interview-preparation-kit/ctci-fibonacci-numbers.py)|
510+
|Recursion and Backtracking|Medium|[Recursion: Davis' Staircase](https://www.hackerrank.com/challenges/ctci-recursive-staircase/problem)|[ctci-recursive-staircase.py](interview-preparation-kit/ctci-recursive-staircase.py)|
511+
|Recursion and Backtracking|Medium|[Recursive Digit Sum](https://www.hackerrank.com/challenges/recursive-digit-sum/problem)|[recursive-digit-sum.py](interview-preparation-kit/recursive-digit-sum.py)|
512+
|Search|Medium|[Hash Tables: Ice Cream Parlor](https://www.hackerrank.com/challenges/ctci-ice-cream-parlor/problem)|[ctci-ice-cream-parlor.py](interview-preparation-kit/ctci-ice-cream-parlor.py)|
513+
|Search|Medium|[Swap Nodes [Algo]](https://www.hackerrank.com/challenges/swap-nodes-algo/problem)|[swap-nodes-algo.py](interview-preparation-kit/swap-nodes-algo.py)|
514+
|Sorting|Easy|[Mark and Toys](https://www.hackerrank.com/challenges/mark-and-toys/problem)|[mark-and-toys.py](interview-preparation-kit/mark-and-toys.py)|
515+
|Sorting|Easy|[Sorting: Bubble Sort](https://www.hackerrank.com/challenges/ctci-bubble-sort/problem)|[ctci-bubble-sort.py](interview-preparation-kit/ctci-bubble-sort.py)|
516+
|Sorting|Medium|[Fraudulent Activity Notifications](https://www.hackerrank.com/challenges/fraudulent-activity-notifications/problem)|[fraudulent-activity-notifications.py](interview-preparation-kit/fraudulent-activity-notifications.py)|
517+
|Sorting|Medium|[Sorting: Comparator](https://www.hackerrank.com/challenges/ctci-comparator-sorting/problem)|[ctci-comparator-sorting.py](interview-preparation-kit/ctci-comparator-sorting.py)|
518+
|Sorting|Hard|[Merge Sort: Counting Inversions](https://www.hackerrank.com/challenges/ctci-merge-sort/problem)|[ctci-merge-sort.py](interview-preparation-kit/ctci-merge-sort.py)|
519+
|Stacks and Queues|Medium|[Balanced Brackets](https://www.hackerrank.com/challenges/balanced-brackets/problem)|[balanced-brackets.py](interview-preparation-kit/balanced-brackets.py)|
520+
|Stacks and Queues|Medium|[Castle on the Grid](https://www.hackerrank.com/challenges/castle-on-the-grid/problem)|[castle-on-the-grid.py](interview-preparation-kit/castle-on-the-grid.py)|
521+
|Stacks and Queues|Medium|[Largest Rectangle](https://www.hackerrank.com/challenges/largest-rectangle/problem)|[largest-rectangle.py](interview-preparation-kit/largest-rectangle.py)|
522+
|Stacks and Queues|Medium|[Queues: A Tale of Two Stacks](https://www.hackerrank.com/challenges/ctci-queue-using-two-stacks/problem)|[ctci-queue-using-two-stacks.py](interview-preparation-kit/ctci-queue-using-two-stacks.py)|
523+
|Stacks and Queues|Hard|[Poisonous Plants](https://www.hackerrank.com/challenges/poisonous-plants/problem)|[poisonous-plants.py](interview-preparation-kit/poisonous-plants.py)|
524+
|Strings|Easy|[Alternating Characters](https://www.hackerrank.com/challenges/alternating-characters/problem)|[alternating-characters.py](interview-preparation-kit/alternating-characters.py)|
525+
|Strings|Easy|[Strings: Making Anagrams](https://www.hackerrank.com/challenges/ctci-making-anagrams/problem)|[ctci-making-anagrams.py](interview-preparation-kit/ctci-making-anagrams.py)|
526+
|Trees|Easy|[Binary Search Tree : Lowest Common Ancestor](https://www.hackerrank.com/challenges/binary-search-tree-lowest-common-ancestor/problem)|[binary-search-tree-lowest-common-ancestor.py](interview-preparation-kit/binary-search-tree-lowest-common-ancestor.py)|
527+
|Trees|Easy|[Tree: Height of a Binary Tree](https://www.hackerrank.com/challenges/tree-height-of-a-binary-tree/problem)|[tree-height-of-a-binary-tree.py](interview-preparation-kit/tree-height-of-a-binary-tree.py)|
528+
|Trees|Medium|[Tree: Huffman Decoding](https://www.hackerrank.com/challenges/tree-huffman-decoding/problem)|[tree-huffman-decoding.py](interview-preparation-kit/tree-huffman-decoding.py)|
529+
|Trees|Medium|[Trees: Is This a Binary Search Tree?](https://www.hackerrank.com/challenges/ctci-is-binary-search-tree/problem)|[ctci-is-binary-search-tree.py](interview-preparation-kit/ctci-is-binary-search-tree.py)|

interview-preparation-kit/2d-array.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/python3
2+
3+
import sys
4+
5+
6+
arr = []
7+
for arr_i in range(6):
8+
arr_t = [int(arr_temp) for arr_temp in input().strip().split(' ')]
9+
arr.append(arr_t)
10+
11+
12+
best = 0
13+
for ind_i in range(6-2):
14+
for ind_j in range(6-2):
15+
temp = 0
16+
temp += arr[ind_i][ind_j] + arr[ind_i][ind_j+1] + arr[ind_i][ind_j+2]
17+
temp += arr[ind_i+1][ind_j+1]
18+
temp += arr[ind_i+2][ind_j] + arr[ind_i+2][ind_j+1] + arr[ind_i+2][ind_j+2]
19+
if ind_i == 0 and ind_j == 0:
20+
best = temp
21+
else:
22+
best = max(best, temp)
23+
24+
print(best)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/python3
2+
3+
import sys
4+
5+
def alternatingCharacters(s):
6+
string = list(s)
7+
last = string.pop()
8+
res = 0
9+
10+
while string:
11+
newone = string.pop()
12+
if newone == last:
13+
res += 1
14+
else:
15+
last = newone
16+
17+
return res
18+
19+
q = int(input().strip())
20+
for a0 in range(q):
21+
s = input().strip()
22+
result = alternatingCharacters(s)
23+
print(result)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/python3
2+
3+
import sys
4+
5+
def angryChildren(k, arr):
6+
arr = sorted(arr)
7+
res = arr[-1]
8+
9+
for ind in range(len(arr)-k+1):
10+
res = min(res, arr[ind+k-1] - arr[ind])
11+
#print("ind = {} arr = {} res = {}".format(ind, arr[ind:ind+k], res))
12+
13+
return res
14+
15+
16+
if __name__ == "__main__":
17+
n = int(input().strip())
18+
k = int(input().strip())
19+
arr = []
20+
arr_i = 0
21+
for arr_i in range(n):
22+
arr_t = int(input().strip())
23+
arr.append(arr_t)
24+
result = angryChildren(k, arr)
25+
print(result)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/python3
2+
3+
import sys
4+
5+
def isBalanced(s):
6+
stack = []
7+
8+
for letter in s:
9+
if letter == '{':
10+
stack.append(1)
11+
elif letter == '[':
12+
stack.append(2)
13+
elif letter == '(':
14+
stack.append(3)
15+
elif letter == '}':
16+
if len(stack) == 0:
17+
return False
18+
if stack.pop() != 1:
19+
return False
20+
elif letter == ']':
21+
if len(stack) == 0:
22+
return False
23+
if stack.pop() != 2:
24+
return False
25+
elif letter == ')':
26+
if len(stack) == 0:
27+
return False
28+
if stack.pop() != 3:
29+
return False
30+
31+
return len(stack) == 0
32+
33+
if __name__ == "__main__":
34+
t = int(input().strip())
35+
for a0 in range(t):
36+
s = input().strip()
37+
result = isBalanced(s)
38+
if result is True:
39+
print('YES')
40+
else:
41+
print('NO')
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
Node is defined as
3+
self.left (the left child of the node)
4+
self.right (the right child of the node)
5+
self.data (the value of the node)
6+
"""
7+
8+
def lca(root , v1 , v2):
9+
vals = sorted([v1, v2])
10+
v1, v2 = vals[0], vals[1]
11+
node = root
12+
while True:
13+
if v1 < v2 < node.data:
14+
node = node.left
15+
if v1 > v2 > node.data:
16+
node = node.right
17+
if v1 <= node.data <= v2:
18+
break
19+
return node
20+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
from collections import deque
5+
6+
def is_safe(grid, x, y, distances):
7+
return x >= 0 and x < len(grid) and y >= 0 and y < len(grid) and distances[x][y] == -1 and grid[x][y] != 'X'
8+
9+
def get_safe_moves(grid, node, distances):
10+
directions = [(-1, 0), (0, 1), (1, 0), (0, -1)]
11+
variants = []
12+
13+
for di in directions:
14+
nunode = (node[0] + di[0], node[1] + di[1])
15+
while is_safe(grid, nunode[0], nunode[1], distances):
16+
variants.append(nunode)
17+
nunode = (nunode[0] + di[0], nunode[1] + di[1])
18+
19+
return variants
20+
21+
22+
def minimumMoves(grid, startX, startY, goalX, goalY):
23+
next_to_visit = deque()
24+
node = (startX, startY)
25+
next_to_visit.appendleft(node)
26+
distances = [[-1]*len(grid) for _ in range(len(grid))]
27+
distances[startX][startY] = 0
28+
29+
while next_to_visit:
30+
node = next_to_visit.pop()
31+
#print("point = ({}, {})".format(node[0], node[1]))
32+
#for row in distances:
33+
# print(row)
34+
#print()
35+
height = distances[node[0]][node[1]]
36+
37+
variants = get_safe_moves(grid, node, distances)
38+
39+
for var in variants:
40+
if var == (goalX, goalY):
41+
return height + 1
42+
distances[var[0]][var[1]] = height + 1
43+
next_to_visit.appendleft(var)
44+
45+
return -1
46+
47+
48+
if __name__ == "__main__":
49+
n = int(input().strip())
50+
grid = []
51+
for _ in range(n):
52+
layer = list(input().strip())
53+
grid.append(layer)
54+
startX, startY, goalX, goalY = [int(i) for i in input().strip().split()]
55+
result = minimumMoves(grid, startX, startY, goalX, goalY)
56+
print(result)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/python3
2+
3+
import math
4+
import os
5+
import random
6+
import re
7+
import sys
8+
from collections import defaultdict
9+
10+
# Complete the countTriplets function below.
11+
def countTriplets(arr, r):
12+
res = 0
13+
pairs = defaultdict(int)
14+
triplets = defaultdict(int)
15+
16+
for el in arr:
17+
res += triplets[el]
18+
19+
triplets[r*el] += pairs[el]
20+
pairs[r*el] += 1
21+
#print("el = {} triplets = {} pairs = {}".format(el, dict(triplets), dict(pairs)))
22+
23+
return res
24+
25+
if __name__ == '__main__':
26+
fptr = open(os.environ['OUTPUT_PATH'], 'w')
27+
28+
nr = input().rstrip().split()
29+
30+
n = int(nr[0])
31+
32+
r = int(nr[1])
33+
34+
arr = list(map(int, input().rstrip().split()))
35+
36+
ans = countTriplets(arr, r)
37+
38+
fptr.write(str(ans) + '\n')
39+
40+
fptr.close()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/python3
2+
3+
import sys
4+
5+
def countingValleys(n, s):
6+
res = 0
7+
in_valley = 0
8+
curr = 0
9+
10+
for step in s:
11+
if step == 'U':
12+
curr += 1
13+
else:
14+
curr -= 1
15+
16+
if curr < 0 and in_valley == 0:
17+
in_valley = 1
18+
if in_valley == 1 and curr == 0:
19+
in_valley = 0
20+
res += 1
21+
22+
return res
23+
24+
if __name__ == "__main__":
25+
n = int(input().strip())
26+
s = input().strip()
27+
result = countingValleys(n, s)
28+
print(result)

interview-preparation-kit/crush.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/python3
2+
3+
import sys
4+
5+
if __name__ == "__main__":
6+
n, m = input().strip().split(' ')
7+
n, m = [int(n), int(m)]
8+
array = [0] * (n+1)
9+
for a0 in range(m):
10+
a, b, k = input().strip().split(' ')
11+
a, b, k = [int(a), int(b), int(k)]
12+
13+
array[a-1] += k
14+
if b+1 <= n:
15+
array[b] -= k
16+
17+
res_max = 0
18+
res = 0
19+
for dif in array:
20+
res += dif
21+
res_max = max(res_max, res)
22+
23+
print(res_max)
24+
25+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <math.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include <stdlib.h>
5+
#include <assert.h>
6+
#include <limits.h>
7+
#include <stdbool.h>
8+
9+
int main() {
10+
int n;
11+
int k;
12+
13+
scanf("%d %d",&n,&k);
14+
15+
int *a = malloc(sizeof(int) * n);
16+
int *a_out = malloc(sizeof(int) * n);
17+
18+
for(int a_i = 0; a_i < n; a_i++){
19+
scanf("%d",&a[a_i]);
20+
}
21+
/*
22+
for(int i = 0; i < k; i++){
23+
int buf = a[0];
24+
25+
for (int j = 0; j < n-1; j++) {
26+
a[j] = a[j+1];
27+
}
28+
29+
a[n-1] = buf;
30+
}
31+
*/
32+
for (int i = 0; i < n; i++) {
33+
a_out[(i+n-k)%n] = a[i];
34+
}
35+
for(int a_i = 0; a_i < n; a_i++){
36+
printf("%d ", a_out[a_i]);
37+
}
38+
printf("\n");
39+
40+
free(a);
41+
return 0;
42+
}

0 commit comments

Comments
 (0)