December 2019 Flashcards
Difference b/t pass and continue
pass is a noop whereas continue skips to the next iteration of the loop
What to do to account for potential middle character in palindrome check?
Nothing. The following code will account for the middle value:
if string[i] == string[len(string)-i-1]:
` return False `
python equivalent of
indexOf
index
myList.index(“a”)
Do you need to perform a cast in order to store an int as a key in a dictionary?
No, you can declare:
nums = {}
for nums in array:
` nums[num] = True`
How to iterate in reversed order?
for i in reversed(range(0, len(arr))):
` print(i)`
How to make a mirror ‘visited’ matrix based on an input matrix?
visited =
[[False for value in row] for row in matrix]
How would you iterate in reverse by two given the following array:
array = [1,2,3,4,5]
for i in reversed(range(0, len(l), 2):
In dynamic programming, how do you form the initial grid for something like the Levenshtein Distance algorithm?
str1 = "abc"
str2 = "yabd"
edits = [[x for x in range(len(str1)+1)] for row in range(len(str2)+1)]
result:
[
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3]
]
what are the values for variables
a and b?
test = [1,2,3,4,5,6]
a = [:2]
b = [2:]
a: [1,2]
b: [3,4,5,6]
Describe Levenshstein Distance
Problem
Find the minimum number of edits required to change one string to another. The following options are available:
- remove
- delete
- insert
Solution
- create memoization grid based on lengths of input strings
- seed the first row
- seed the first column
- iterate through nested loop (str1, str2)
- if char of str2 == char of str1 memo value = diagonal (top left)
- else memo value = 1 + memo of top, left, topleft
Analysis
O(mn) time
O(m,n) space but O(min(m,n)) possible
Whatis Kadane’s Algorithm
Problem
The input is a non empty array of numbers. The target output is the greatest sum found within a subarray of the input array.
Solution
The key variables to track and initialize are
maxEndingHere = array[0]
maxSoFar = array[0]
You iterate through the array and update the variables. The key concept is to realize that you are update the values based on the largest subarray which ends at a given index.
Analysis
O(n) time
O(1) space
Describe the branch sums problem. How do you solve for it?
Problem
The question asks that you populate an array of sums based on the branches of a given binary search tree.
Solution
Create a helper method which takes a node, solution array, and ‘runningSum’.
- Return if node is null.
- Otherwise calculate new runningSum based on node value.
- If there is no left child and no right child, append updated runningSum to array.
- Else, recurse on child left and recurse on child right
Analysis
O(n) time
O(n) space
Describe the 3 largest problem. Why would you not want a sort operation?
Problem
Given an array, find the three largest values.
Solution
You initialize a three item solution array threeLargest
in which each item is negative infinity.
Analysis
O(n) time
O(1) space
Then, create a helper function which takes inthreeLargest
and a number. For each item inthreeLargest
, see if the number is greater than the current item. If it is, adjust threeLargest
so that its values are shifted appropriately and the new value is placed at the correct position. You can implement a shiftArrAndPlaceNum(arr, val, index) function to achieve this end.
The reason you don’t want to use a sorting operation is that the aforementioned implementation has a O(n) time complexity whereas sorting means at least O(nlog n)
Describe the two number sum problem.
Problem
Given an array of numbers and a target sum, return an array of the two numbers from the input array which add up to the target sum.
Solution
- sort array
- initialize left and right pointers for ends of the array
- while left < right:
- candidate = sum of left and right pointers
- if candidate == target, return [left, right]
- elif candidate < target: left += 1
- elif candidate > target: right -= 1
Analysis
O(n) time
O(n) space
Describe the three number sum problem
Problem
Given an array and a target sum, find all triplets which will equal the target sum.
Solution
- sort array
- for num in array
- create pointers left and right based on num position
- while left < right
- find sum of all three values
- if sum == target, left++ right++ and append into solution array
- if sum < target, left ++
- if sum > target, right–
- while left < right
- create pointers left and right based on num position
- return solution array
Analysis
O(n^2) time
O(n) space where n is length of input array