Skip to content

Commit

Permalink
coding interview 2018 added
Browse files Browse the repository at this point in the history
  • Loading branch information
minsuk-heo committed Jun 23, 2018
1 parent 6eec585 commit 83f6c47
Show file tree
Hide file tree
Showing 23 changed files with 1,799 additions and 0 deletions.
11 changes: 11 additions & 0 deletions coding_interview_2018/.idea/coding_interview_2017.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions coding_interview_2018/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions coding_interview_2018/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

930 changes: 930 additions & 0 deletions coding_interview_2018/.idea/workspace.xml

Large diffs are not rendered by default.

84 changes: 84 additions & 0 deletions coding_interview_2018/binary_search/find_k_rotated_sorted_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""
[1,2,3,4,5] k=4, return 3
[1,2,3,4,5] k=6, return -1
[4,5,1,2,3] k=4, return 0
"""

def find_pivot(arr,l, r):
if len(arr) < 1:
return -1
if arr[l] < arr[r]:
return -1

while l < r:
mid = (l + r) // 2
if arr[mid] < arr[mid-1]:
return mid -1
else:
if arr[mid] > arr[l]:
l = mid
else:
r = mid - 1
return l


def find_k(arr, pivot, k):
if arr[pivot] == k :
return pivot

if pivot == -1:
#binary search on arr
l, r = 0, len(arr)-1
elif pivot == 0:
l, r = pivot+1, len(arr)-1
else:
#binary search considering pivot
if k >= arr[0] and k < arr[pivot]:
l, r = 0, pivot-1
else:
l, r = pivot+1, len(arr)-1

while l < r:
mid = (l + r) // 2
if arr[mid] == k:
return mid
else:
if arr[mid] < k:
l = mid + 1
else:
r = mid - 1

return l if arr[l] == k else -1


arr = [5,1,2,3,4]
pivot = find_pivot(arr, 0 ,len(arr)-1)
#print(pivot)
#print(find_k(arr, pivot, 4))


arr = [4,5,1,2,3]
pivot = find_pivot(arr, 0 ,len(arr)-1)
#print(pivot)
#print(find_k(arr, pivot, 4))


arr = [3,4,5,1,2]
pivot = find_pivot(arr, 0 ,len(arr)-1)
#print(pivot)
#print(find_k(arr, pivot, 4))

arr = [2,3,4,5,1]
pivot = find_pivot(arr, 0 ,len(arr)-1)
#print(pivot)
#print(find_k(arr, pivot, 4))

arr = [1,2,3,4,5]
pivot = find_pivot(arr, 0 ,len(arr)-1)
#print(pivot)
#print(find_k(arr, pivot, 4))

arr = [1,2,3,4,5]
pivot = find_pivot(arr, 0 ,len(arr)-1)
print(pivot)
print(find_k(arr, pivot, 40))
38 changes: 38 additions & 0 deletions coding_interview_2018/binary_search/first_failed_build_ver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
S,S,S,F,F,F,F,F,F,F
"""


def binary_search(input, l, r):
if len(input) < 1:
return -1

while l < r:
mid = (l + r) // 2
if input[mid] == "F":
r = mid
else:
l = mid + 1

return l if input[l] == "F" else -1


input = ["S", "S", "S", "F", "F", "F", "F", "F", "F", "F"]
idx = binary_search(input, 0 , len(input)-1)
print(idx)

input = ["F", "F", "F"]
idx = binary_search(input, 0 , len(input)-1)
print(idx)

input = ["S", "S", "S"]
idx = binary_search(input, 0 , len(input)-1)
print(idx)

input = ["S"]
idx = binary_search(input, 0 , len(input)-1)
print(idx)

input = []
idx = binary_search(input, 0 , len(input)-1)
print(idx)
39 changes: 39 additions & 0 deletions coding_interview_2018/data_structure/trie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
store word in dictionary
search word in dictionary return True if exist
"""

class Trie:
head = {}

def add(self, word):
cur = self.head
for ch in word:
if ch not in cur:
cur[ch] = {}
cur = cur[ch]
# item denotes the Trie has this word as item
# if item key doesn't exist Trie doesn't have this word but as path to longer word
cur['*'] = True

def search(self, word):
cur = self.head
for ch in word:
if ch not in cur:
return False
cur = cur[ch]

if '*' in cur:
return True
else:
return False


dictionary = Trie()

dictionary.add("hi")
dictionary.add("hello")
print(dictionary.search("hi"))
print(dictionary.search("hello"))
print(dictionary.search("hel"))
print(dictionary.search("hey"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Floyd Warshall Algorithm
3
+-------+
| |
| 2 v
(0)<----(3)-------+
| ^ |
2 | | 6 | 2
v | |
(1)---->(2)<------+
4
"""
# I is infinity
I = 99999
matrix = [[0,2,I,3],
[I,0,4,I],
[I,I,0,6],
[2,I,2,0]]
23 changes: 23 additions & 0 deletions coding_interview_2018/dynamic_programming/largest_sum_subarray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
f([-2,-1,4,1]) = 5
f([-2,-1,4,1,-3,1]) = 5
"""

def largest_sum_subarray(arr):
l_sum = arr[0]
cur = arr[0]
for i in range(1, len(arr)):
if cur > 0:
cur = cur + arr[i]
else:
cur = arr[i]

l_sum = max(l_sum, cur)

return l_sum


print( largest_sum_subarray( [-2,-1] ) )
print( largest_sum_subarray( [-2,-1,4,1] ) )
print( largest_sum_subarray( [-2,-1,4,1,-3,1] ) )
print( largest_sum_subarray( [-2,-1,4,1,-1,3] ) )
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Returns the length of the longest palindromic subsequence

def longest_palindrome_subsequence(str):
n = len(str)

# Create a table to store results of subproblems
matrix = []
for i in range(n):
matrix.append([0]*n)

# Strings of length 1 are palindrome of length 1
for i in range(n):
matrix[i][i] = 1

# window is length of substring
for window in range(2, n + 1):
for i in range(n - window + 1):
j = i + window - 1
if str[i] == str[j] and window == 2:
matrix[i][j] = 2
elif str[i] == str[j]:
matrix[i][j] = matrix[i + 1][j - 1] + 2
else:
matrix[i][j] = max(matrix[i][j - 1], matrix[i + 1][j]);

return matrix[0][n - 1]

print(longest_palindrome_subsequence("a"))
print(longest_palindrome_subsequence("abba"))
print(longest_palindrome_subsequence("efet"))
print(longest_palindrome_subsequence("abcdcba"))
print(longest_palindrome_subsequence("abcddcba"))
print(longest_palindrome_subsequence("awbcdedcba"))
36 changes: 36 additions & 0 deletions coding_interview_2018/etc/longest_palindrome_substring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
f("a") = "a"
f("aa") = "aa"
f("aba") = "aba"
f("abba") = "abba"
f("abcdc") = "cdc"
"""


def find_pal(left, right, input):
if right >= len(input):
return ""
if input[left] != input[right]:
return ""
while left >= 0 and right < len(input) and input[left] == input[right]:
left -= 1
right += 1

return input[left+1:right]


def longest_palindrome(input):
if len(input) <= 1:
return input
max_odd = ""
max_even = ""
for idx in range(len(input)):
max_odd = max(max_odd, find_pal(idx, idx, input))
max_even = max(max_even, find_pal(idx, idx+1, input))
return max(max_odd, max_even)

print( longest_palindrome("") )
print( longest_palindrome("a") )
print( longest_palindrome("aa") )
print( longest_palindrome("aba") )
print( longest_palindrome("abcdc") )
30 changes: 30 additions & 0 deletions coding_interview_2018/etc/longest_substring_k_uniq_ch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
abaydae = 5
abcade = 6
"""


def longest_substring(str, k):
log = {}
cur = ""
largest = 0

for i in range(len(str)):
if not str[i] in log:
log[str[i]] = 1
cur += str[i]
else:
if log[str[i]] == k:
largest = max(largest, len(cur))
start = cur.find(str[i])
cur = cur[start+1:] + str[i]
else:
log[str[i]] += 1
cur += str[i]
return max(largest, len(cur))

str ="aaaaaaa"
print( longest_substring(str,2) )

str ="abaa"
print( longest_substring(str,1) )
Loading

0 comments on commit 83f6c47

Please sign in to comment.