General Flashcards Preview

Grokking Algorithms > General > Flashcards

Flashcards in General Deck (10)
Loading flashcards...
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

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

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)
4
Q

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

A
  1. high
  2. mid
  3. low
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.

6
Q

What is the basic outline for quicksorting?

A

Recursive divide and conquer algorithm:

quicksort(smaller) + [pivot] + quicksort(larger)
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]
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.

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):

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`