Algorithms Flashcards

1
Q

Tree: Number of nodes in a full binary tree with L levels?

A

Nodes = (2 ^ L) - 1

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

Tree: What numbers of calls does recursive function with branching do with input N?

A

Ncalls = (Branches ^ Levels) - 1, where

Levels = ceiling(N / Decrement) - BaseCaseValue + 1
Levels = ceiling(logN) - ceiling(logBaseCaseValue) + 1
Example:
def f(N):
  if N <= 0:  # BaseCaseValue = 0
    return 1
  return f(N-1) + f(N-1)  # Branches = 2, Decrement = 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Tree: Number of leaves in a full tree with L levels?

A

Leaves = branches ^ (L - 1)

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

Tree: In a complete binary tree represented as an array what is index of children of a node with index N?

A

Left child: 2 * N + 1

Right child: 2 * N + 2

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

KMP: What is KMP algorithm?

A

KMP stands for Knuth-Moris-Pratt and it is an efficient algorithm for finding specific substring in a given string.

Time complexity: O(n + m), where n is a length of the string and m is a length of the substring.

Space complexity: O(m)

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

KMP: In the context of KMP algorithm what will the P-table be for the pattern “abcdabca” ?

A

0 1 2 3 4 5 6 7
a b c d a b c a
[0, 0, 0, 0, 1, 2, 3, 1]

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

KMP: In the context of KMP algorithm what a value inside P-table means?

A

Every value in the table means how many characters in the prefix are already matched. Those values help us to skip comparisons which we already know will succeed, and that is where the efficiency of KMP comes from.

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

Bit tricks: Isolate the first set bit from the right?

A

x = x & ~(x - 1)

Example:
x = 1010

  1. (x - 1) = 1001
  2. ~(x - 1) = 0110
  3. x & ~(x - 1) = 0010
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Bit tricks: Unset the last set bit from the right?

A

x = x & (x - 1)

Example:
x = 1110
1. (x - 1) = 1101
2. x & (x - 1) = 1100

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

What is LCM of two numbers and how to find it?

A

LCM stands for least common multiple and it is a first number that is divisible by the given two numbers A and B.
LCM(a, b) = (a * b) / GCD(a, b)

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

What is the Euclidean Algorithm?

A

It is an efficient algorithm for finding GCD:

r = a % b
while r:
 a = b
 b = r
 r = a % b
return b
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Graph: What are common algorithms used to solve shortest path problem in graphs?

A
  • BFS (unweighted graphs)
  • Dijkstra (without negative weights)
  • Bellman-Ford
  • Floyd-Warshall
  • A*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Graph: What are common algorithms used to solve connectivity problem in graphs (does there exist path from one node to another)?

A
  • use union find data structure

- DFS

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

Graph: What are common algorithms used to detect negative cycles?

A
  • Bellman-Ford

- Floyd-Warshall

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

Graph: What are common algorithms used to find strongly connected components in a directed graph?

A
  • Tarjan’s algorithm

- Kosaraju’s algorithm

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

Graph: What are common algorithms used to find a Minimum Spanning Tree (MST) of a graph?

A
  • Kruskal’s algorithm
  • Prim’s algorithm
  • Boruvka’s algorithm
17
Q

Graph: What is a time and space complexity of DFS algorithm?

A

Time: O(V + E)
Space: O(E)

18
Q

Graph: What is a time and space complexity of BFS algorithm?

A

Time: O(V + E)
Space: O(E)

19
Q

Graph: What is Dijkstra’s Algorithm and what its time and space complexity?

A

Dijkstra’s algorithm is a Single Source Shortest Path (SSSP) greedy algorithm for graphs with non-negative edge weights.

Typical time complexity: O(E*log(V))
Space complexity: O(E)

20
Q

Graph: What are Lazy Dijkstra’s and Eager Dijkstra’s?

A

Those are two different implementations of the Dijkstra’s algorithm.

Lazy Dijkstra’s allows duplicate key values in the priority queue, while Eager Dijkstra’s uses Indexed Priority Queue (IPQ) to change priority (distance) of a node instead of inserting a duplicate key.

Lazy implementation is less efficient on dense graphs.

21
Q

What general equation of a linear congruential generator?

A

seed = (a * seed + c) mod m,
Where a, c and m are specially picked constants.

For range [0, 1): seed / m
For range [l, h]: floor(l + (h - l + 1) * seed / m)