Algos Flashcards
1
Q
What does a binary search tree node constructor look like?
A
constructor(data) { this.data = data; this.right = null; this.left = null; }
2
Q
What are the steps of a binary search tree node insert method?
A
- Set the current node to the root
- While true, if the value is less than the currentNode.value and there is no currentNode.left, set to left
- Otherwise set currentNode to currentNode.left
3
Q
What are the steps of a binary search tree node contains method?
A
- If the data is less than root.value, set currentNode to root.left.
- While true, if value === currentNode.value, return node
4
Q
What does the fibonacci solution look like?
A
function fibonacci(n) {
if (n <= 1) {
return n;
} else {
return (fibonacci(n - 1) + fibonacci(n - 2));
}
}
- Then return fib(n - 1) + fib(n - 2)
5
Q
What are the 5 steps to find maxSubArraySum using sliding window?
A
- set two variables, one for currentSum, one for maxSumFound
- write a traditional for loop over the nums array
- increment currentSum with nums[i]
- if i >= the sub size -1, update maxSumFound by using Math.max
- decrement currentSum with [i - size - 1]
6
Q
What are the 5 steps to implement a BST insert method?
A
- create a new node with the value passed in and set it to root if root is null
- set currentNode to root
- create a while true loop
- if the value is < currentNode.value and currentNode.left is null, set to currentNode.left
- Otherwise currentNode.left is the new currentNode
7
Q
What are the 7 steps for breadth first search?
A
- set currentNode to this.root
- have two array variables, one for list, one for queue, and immediately push currentNode to the queue
- while queue.length > 0
- update currentNode to be queue.shift()
- push the currentNode.value to the list
- if currentNode.left or right exists, push those to the queue
- return the list
8
Q
What are the 6 steps for depth first search?
A
- create a depthFirstSearch function that calls a recursive function
- it will pass the root and and empty array as args
- the recursive function, traverse for example, will call itself “traverse” recursively with node.left if left exists
- then push the node.value to the list
- then traverse the right as you did with left
- return the list
9
Q
What change would you make between inOrder, preOrder, and postOrder DFS?
A
- inOrder, you push the node.value to the list between traversing left and right
- preOrder, you push the node.value to the list before you traverse anything
- postOrder, you push node.value to the list after you traverse everything