Big O Notation Flashcards

1
Q

What is Big O Notation?

A

the language we use to talk about how efficient an algorithm is

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

What is constant time?

A

The time doesn’t change relative to input size

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

What is the notation for constant time?

A

O(1)

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

O(1) is what BigO notation?

A

Constant Time

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

What is linear time?

A

The time will change at the same rate as the input

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

Give a code example of linear time

A

A func that prints every item of an array:

func printAllItems(in items: [Int]) {
for item in items {
print(item)
}
}

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

Give a code example of constant time

A

Accessing one element in an array by index:

func printFirstItem(in items: [Int]) {
print(items[0])
}

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

What is the notation for linear time?

A

O(n)

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

O(n) is what BigO notation?

A

Linear time

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

What is quadratic time?

A

The time required to execute a command is square of the input

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

What is the notation for quadratic notation?

A

O(n^2)

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

O(n^2) is what BigO notation?

A

Quadratic notation

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

Give a code example of quadratic notation?

A

Iterating over nested loops:

func printAllPossibleOrderedPairs(in items: [Int]) {
for firstItem in items {
for secondItem in items {
print(firstItem, secondItem)
}
}
}

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

Is BigO usually concerned with Worst, Average, or Best case scenarios?

A

Worst

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

What are the two complexities that BigO can refer to?

A

Time and Space

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

Identify the time complexity:

func printAllPossibleOrderedPairs(in items: [Int]) {
for firstItem in items {
for secondItem in items {
print(firstItem, secondItem)
}
}
}

A

Quadratic, O(n^2), because theres nested loops

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

Identify the time complexity:

Accessing one element in an array by index:

func printFirstItem(in items: [Int]) {
print(items[0])
}

A

O(1), constant

18
Q

Identify the time complexity:

func printAllItems(in items: [Int]) {
for item in items {
print(item)
}
}

A

Linear, O(n)

19
Q

Free card

A

Have some good karma

20
Q

What is the time complexity of an array lookup by index?

21
Q

What is the time complexity of a hash lookup?

A

O(1) on average bc of potential collisions

22
Q

What is x?

x = log(2, 16)

23
Q

What is x?

x = log(3, 9)

24
Q

What is x?

x = log(5, 25)

25
What is the notation for a logarithmic algorithm?
O(log n)
26
O(log n) is what BigO notation?
Logarithmic
27
Give an example of a logarithmic algorithm and explain why
Binary Search because each step exponentially reduces the input size
28
A logarithm is the inverse function to what?
exponents
29
What is the time complexity of binary search? why?
O(log n) because the search input decreases in size logarithmically
30
Identify the bigO notation and explain why for i in stride(from: 0, to: n, by: 1) { for j in stride(from: 1, to: n, by: 1) { for k in stride(from: 1, to: n, by: 1) { // do constant time stuff } } }
O(n^3), cubic, because theres 3 levels of nested loops
31
Identify the notation: it only takes a single step for the algorithm to accomplish the task.
Constant: O(1)
32
Identify the notation: The number of steps it takes to accomplish a task are decreased by some factor with each step.
Logarithmic: O(log n)
33
Identify the notation: The number of of steps required are directly related to the input
Linear: O(n)
34
Identify the notation: The number of steps it takes to accomplish a task is square of n
Quadratic: O(n^2)
35
Identify the notation: The number of steps it takes to accomplish a task is a constant to the n power (pretty large number)
Exponential: O(k^n)
36
What is a logarithm?
The power that a number needs to be raised to in order to get another number
37
Define: The power that a number needs to be raised to in order to get another number
Logarithm
38
What is the time complexity for an algorithm that calculates a Fibonacci number using recursion? and why?
exponential, o(2^n), because each iteration calls itself twice. Because the iteration returns ( fib(n-1) + fib(n-2) ).
39
Which function is the inverse of exponential?
Logarithmic
40
Which function is the inverse of logarithmic?
Exponential
41
Whats the difference between quadratic notation and exponential notation?
quadratic == n^2 exponential == x^y