Searching + DFS + BFS Flashcards

1
Q

What is an important requirement with the data when using a binary search algorithm?

A

The data needs to be sorted

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the average case TC for a binary search?

A

O(logn)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the average case TC for traversing a tree or graph?

A

O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

If graph/tree traversal is

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What order would this tree be searched with DFS?
9
4 20
1 6 15 170

A

9, 4, 20, 1, 6, 15, 170

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why does BFS require additional memory?

A

Because it needs to track all the child nodes on a specific level as it searches that level

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What order would this tree be searched with DFS?
9
4 20
1 6 15 170

A

9, 4, 1, 6, 20, 15, 170

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What would you use if you know a solution is not far from the root of a tree?

A

BFS because it starts at the closest nodes from the top

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What would you use if the tree is very deep and solutions are rare?

A

BFS, because DFS would end up taking a very long time to traverse…?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What would you use if the tree is very wide?

A

DFS, because BFS will need too much memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What would you use if solutions are frequent but located deep in the tree?

A

DFS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What would you use to determine whether a path exists between two nodes?

A

DFS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What data structure should we use to keep track of children nodes in a BFS algorithm?

A

Queue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the three ‘orders’ of a DFS? Perform each on this tree:
9
4 20
1 6 15 170

A
  1. InOrder: [9, 4, 1, 6, 20, 15, 170]
  2. PreOrder: [1, 4, 6, 9, 15, 20, 170]
  3. PostOrder: [1, 6, 4, 15, 170, 20, 9]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the pros and cons of using BFS in a graph?

A

Pros: good for finding shortest path between nodes
Cons: more memory is used to store child nodes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the pros and cons of using DFS in a graph?

A

Pros: less memory to use, good at finding if a path exists between values
Cons: can get slow if it’s a deep graph

17
Q

What is dynamic programming?

A

An optimization technique that uses caching

Can you cache? Then you can use D.P

18
Q

What is caching?

A

Storing values to be used at a later time