General Time Complexity Flashcards

1
Q

What is “Big O” notation?

A

f(n) is O(g(n)) as n → ∞

An upper-bound approximation of the behavior of f(n) as n approaches infinity

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

What are the 6 main types of Big O?

A
  1. O(1)
  2. O(log₂ n)
  3. O(n)
  4. O(n log n)
  5. O(n²)
  6. O(2ⁿ)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the 4 rules of Big O?

A
  1. Assume worst case scenario
  2. Remove constants
  3. Remove non-dominants
  4. Different inputs require different identifiers
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the time complexity for:

obj.method(n, o) {
n.forEach(...);
o.forEach(...);
}
A

O(n + o)

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

What is the time complexity for:

obj.method(n) {
n.forEach(e -> n.forEach(...));
A

O(n^2)

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

What is the time complexity for:

obj.method(n) {
n.forEach(...);
n.forEach(e -> n.forEach(...));
}
A

O(n^2)

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

What is the time complexity for:

obj.method(n, o) {
n.forEach(e -> o.forEach(...));
}
A

O(n * o)

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

What does “amortized constant time” mean?

A

It means O(1) most of the time, but O(n) in rare occasions. An example would be dynamic arrays that have readhed their capacity

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. What is the time complexity of a binary search algorithm?
  2. What are the 2 requirements for using a binary search algorithm?
A
  1. O(log(n))
    1. Sorted data
      Or
    2. A binary decision to reduce search range by half
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the different ways to think about O(log n)?

A
  1. As n doubles, the number of operations increases by 1
  2. The number of operations can be determined by continuously dividing n by 2 until it produces a value <= 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Logarithms are the inverse of…

A

Exponents

For example:
2^3 = 2 * 2 * 2
is the inverse of
log(8) = 8/2 4/2 2/2

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

Could 2 different algorithms with the same Big O have different speeds?

A

Yes

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

Describe how to analyze the time complexity of a recursive algorithm

A

For each recursive invocation, account for the number of operations performed within its body. Multiply that sum by the number fo recursive invocations performed. (This is also how a person would analyze the time complexity of an algorithm that calls another method)

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

What is the purpose of Big O?

A

The purpose of Big O is to classify the computational complexity of an algorithm

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

What is time complexity?

A

Time complexity is the amount of time an algorithm needs to compute relative to the input size

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

What is space complexity?

A

Space complexity is the amount of memory an algorithm needs to compute relative to the input size

17
Q

When dealing with computational complexity, what are the 3 general types of cases?

A
  1. Best case
  2. Average case
  3. Worst case
18
Q

What is the time complexity for most efficient sorting algorithms?

A

O(n * log(n))

19
Q

True or False

We never count the space used by the input (it is bad practice to modify the input), and usually don’t count the space used by the output (the answer) unless an interviewer asks us to

A

True