Algorithms Flashcards

1
Q

time complexity

A

the number of elementary instructions a program executes

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

Big O

A

upper bound on time (asymptotic performance)

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

Big Ω

A

lower bound

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

Big θ

A

both O and Ω

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

3 ways to describe runtime of an algorithm

A

best case
worst case
expected case

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

space complexity

A

amount of memory or space required by an algorithm

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

data structures

A
Linked List
Tree, Trie and Graph
Stack and Queue
Heap
Vector/ArrayList
HashTable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

search and sort algorithms

A
breadth-first search
depth-first search
binary search
merge sort
quick sort
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

steps to handle a technical question

A
  1. Listen carefully
  2. Draw an example
  3. State a brute force
  4. Optimize
  5. Walk through
  6. Implement
  7. Test

Look for bottlenecks, unnecessary work and duplicated work.

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

Linked List

A

runner technique: two pointers, one runs ahead

many linked list problems rely on recursion

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

Stack

A

LIFO: most recent item added is the first to be removed. E.g. stack of dinner plates

Used in some recursive algorithms and to implement a recursive algorithm iteratively.

pop()
push(item)
peek()
isEmpty()

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

Queue

A

FIFO: first-in, first-out. E.g. ticket line
Often used in breadth-first search or caches.

add(item)
remove()
peek()
isEmpty()

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

Tree

A

data structure comprised of nodes. Has a root node with zero or more child nodes. Each child node has zero or more child nodes. Cannot contain cycles.

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

Binary Tree

A

a tree in which each node has up to two children

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

Binary Search Tree

A

a binary tree in which every node has a specific ordering property:
all left descendants <= n < all right descendants

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

Balanced Tree

A

a tree that is “not terribly imbalanced”. Left and right subtrees do not have to be exactly the same size.

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

Complete Binary Tree

A

a binary tree in which every level of the tree is fully filled except for perhaps the last level. The last level is filled left to right.

18
Q

Full Binary Tree

A

a binary tree in which every node has either zero or two children. No node has only one child.

19
Q

Perfect Binary Tree

A

a binary tree that is both full and complete. All leaf nodes will be at the same level, and this level has the maximum number of nodes.

20
Q

In-Order Traversal

A

visit the left branch, the current node, then finally the right branch

21
Q

Pre-Order Traversal

A

visit the current node before its child nodes.

22
Q

Post-Order Traversal

A

visit the current node after its child nodes

23
Q

Min-Heap

A

complete binary tree where each node is smaller than its children. The root is the minimum element in the tree.

24
Q

Max-Heap

A

complete binary tree where each node is larger than its children. The root is the maximum element in the tree.

25
Q

Trie

A

variant of an n-ary tree in which characters are stored at each node. Each path down the tree may represent a word. Also known as Prefix Tree.

26
Q

Graph

A

a collection of nodes with edges between some of them. Can be directed or undirected.

27
Q

Adjacency List

A

most common way to represent a graph. Every node stores a list of adjacent vertices.

28
Q

Depth-First Search

A

search that starts at the root (or other node) and explores each branch completely before moving on to the next branch. Go deep before going wide. Preferred if we want to visit every node in the graph.

29
Q

Breadth-First Search

A

search that starts at the root (or other node) and explores each neighbor before going on to any of their children. Go wide before going deep. Generally better than DFS for finding a path.

30
Q

DFS steps

A
  1. Push the root node on to the stack
  2. Loop until the stack is empty
  3. Peek the stack
  4. If the node has unvisited child nodes, get the unvisited child node, mark it as visited, and push it on the stack
  5. If the node does not have any unvisited child nodes, pop the node from the stack
31
Q

DFS algorithm

A
public void dfs() {
    Stack s = new Stack()
    s.push(rootNode)
    rootNode.visited = true
    print(rootNode)
    while (!s.isEmpty()) {
        Node n = s.peek()
        Node child = getUnvisitedChildNode(n)
        if (child != null) {
            child.visited = true
            print(child)
            s.push(child)
        } else {
            s.pop()
        }
    }
}
32
Q

BFS steps

A
  1. Push the root node on to the queue
  2. Loop until the queue is empty
  3. Remove the node from the queue
  4. If the removed node has unvisited child nodes, mark them as visited and insert the unvisited children in the queue
33
Q

BFS algorithm

A
public void bfs() {
    Queue q = new Queue()
    q.add(root)
    print(root)
    root.visited = true
    while (!q.isEmpty()) {
        Node n - q.remove()
        Node child = null
        while (child = getUnvisitedChildNode(n) != null) {
            child.visited = true
            print(child)
            q.add(child)
        }
    }
}
34
Q

Bottom-Up Approach for recursion

A

start by knowing how to solve for a simple case. Build the solution for a case off the previous one.

35
Q

Top-Down Approach for recursion

A

think about how to divide a problem for a case into subproblems.

36
Q

Half and Half Approach for recursion

A

divide the data set in half. Examples are binary search and merge sort.

37
Q

Dynamic Programming

A

taking a recursive algorithm, finding the repeated calls and caching the results for future recursive calls.

38
Q

Bubble Sort

A

Runtime: O(n²) average and worst case
Memory: O(1)

BubbleSort(A) {
    for (i=0; i A[i+1] {
            swap(A[i], A[i+1])
        }
    }
}
39
Q

Selection Sort

A

Runtime: O(n²) average and worst case
Memory: O(1)

void selection_sort (int A[ ], int n) {

int minimum; 

for (int i = 0; i < n-1 ; i++) {
    minimum = i ;
}
    for (int j = i+1; j < n ; j++ ) {
        if (A[ j ] < A[ minimum ]) { 
        minimum = j ;
    }
}

swap ( A[ minimum ], A[ i ]) {
}

40
Q

Merge Sort

A

Runtime: O(n log (n)) average and worst case
Memory: Depends

Divides the array in half, sorts each half and merges them back together.

MergeSort(arr[], l, r)
If r > l
1. Find the middle point to divide the array into two halves:
middle m = (l+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)

41
Q

Quick Sort

A

Runtime: O(n log (n)) average worst case O(n²)
Memory: O(n log (n))

quickSort(arr[], low, high) {
    if (low < high) {
        pi = partition(arr, low, high);
        quickSort(arr, low, pi - 1); 
        quickSort(arr, pi + 1, high); 
    }
}