Big O Notation Flashcards

1
Q

Why use Big O Notation?

A

To measure and compare the implementation of algorithms.

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

What is Big O?

A

Big O allows us to talk about the runtime of an algorithm as the inputs grow

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

What is the formal definition of Big O?

A

An algorithm is O(f(n)) if the number of simple operations the computer has to do is eventually less than a constant times f(n) as n increases.

Big O measures the worst case of an algorithm: the upper bound.

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

When is an example of O(n^2) occurs?

A

When a O(n) operation is nested in another O(n) operation. This isn’t O(2N) because O(2N) is when two O(N) operations occur sequentially

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

How to measure the complexity of a loop?

A

The complexity of a loop is the length of the loop times the complexity of whatever happens inside of the loop.

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

What are the most common Big O run times in ascending order?

A

O(1) -> O(long n) -> O(n) -> O(n log n) -> O(n^2)

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

What is time complexity?

A

How we analyze the runtime of an algorithm as the size of the inputs increase

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

What is space complexity?

A

How much additional memory do we need to allocate in order to run the code in our algorithm?

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

What are the rules of thumb for Big O time complexity?

A
  1. Arithmetic operations are constant.
    2 Variable assignment is constant.
  2. Accessing elements in an array or object is constant.
  3. In a loop, the complexity is the length of the loop times the complexity of whatever happens inside of the loop
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the rules of thumb for Big O space complexity?

A
  1. Most primitives are constant space.
  2. Strings require O(n) space (where n is the string length)
  3. Reference types are generally O(n) where n is the length (for arrays) or the number of keys (for objects)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is better: O(n) or O(log n)?

A

O(log n)

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