General Flashcards

Some key points to remember from classic algorithms found withing the 'Grokking Algorithms' book (10 cards)

1
Q

For the classic Breadth First Search, (a.) what kind of loop do we implement? (b.) What is the terminating condition for said loop?

A

a. While Loop
b. existence of elements within queue

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

What are two primary functions of a Breadth First Search algorithm?

A

a. Check to see if a path exists between A and B
b. Find shortest path between A and B

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

How do you avoid infinite loops when implementing a Breadth First Search?

A

Maintain and update a ‘searched’ array.

if name not in searched: 
    searched.append(name)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are three key variables to initialize/maintain in the classic binary search?

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

What is a pivot? How do you decide the pivot?

A

It is a randomly selected element within an unsorted list. It is used for quicksort.

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

What is the basic outline for quicksorting?

A

Recursive divide and conquer algorithm:

quicksort(smaller) + [pivot] + quicksort(larger)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Describe BFS process

A

Assume graph exists.

  1. initialize queue
  2. initialize processed array
  3. insert parent node into queue
  4. while queue has elements
    • target = queue.pop
    • if target not in processsed array
    • perform logic ==> t/f
    • and if false, queue += graph[target]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

binary search uses what kind of loop?

A

While loop (while low < high). The trick is that the ‘guess’ is based on the ‘high’ and ‘mid’ values, which are scoped outside the loop and change on every iteration. The ‘mid’ value is scoped within the loop itself.

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

When performing a merge in merge sort, what is the type of loop used? What is the condition necessary to terminate iteration?

A

While Loop, the terminating condition is when the iterators associated with each sub-array are less than the lengths of the sub-arrays. So

while i < len(left) and j < len(right):

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

What is the last step of a merge sort (after the merge)?

A

The last step is adding any remaining elements from sub-arrays. You perform two while loops (not nested). Ex:

while i < len(left):

` arr[k] = left[i]`

` k+=1`

` i+=1`

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