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?

A

O(1)

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)

A

4

23
Q

What is x?

x = log(3, 9)

A

2

24
Q

What is x?

x = log(5, 25)

A

2

25
Q

What is the notation for a logarithmic algorithm?

A

O(log n)

26
Q

O(log n) is what BigO notation?

A

Logarithmic

27
Q

Give an example of a logarithmic algorithm and explain why

A

Binary Search because each step exponentially reduces the input size

28
Q

A logarithm is the inverse function to what?

A

exponents

29
Q

What is the time complexity of binary search? why?

A

O(log n) because the search input decreases in size logarithmically

30
Q

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
}
}
}

A

O(n^3), cubic, because theres 3 levels of nested loops

31
Q

Identify the notation:

it only takes a single step for the algorithm to accomplish the task.

A

Constant: O(1)

32
Q

Identify the notation:

The number of steps it takes to accomplish a task are decreased by some factor with each step.

A

Logarithmic: O(log n)

33
Q

Identify the notation:

The number of of steps required are directly related to the input

A

Linear: O(n)

34
Q

Identify the notation:

The number of steps it takes to accomplish a task is square of n

A

Quadratic: O(n^2)

35
Q

Identify the notation:

The number of steps it takes to accomplish a task is a constant to the n power (pretty large number)

A

Exponential: O(k^n)

36
Q

What is a logarithm?

A

The power that a number needs to be raised to in order to get another number

37
Q

Define: The power that a number needs to be raised to in order to get another number

A

Logarithm

38
Q

What is the time complexity for an algorithm that calculates a Fibonacci number using recursion? and why?

A

exponential, o(2^n), because each iteration calls itself twice. Because the iteration returns ( fib(n-1) + fib(n-2) ).

39
Q

Which function is the inverse of exponential?

A

Logarithmic

40
Q

Which function is the inverse of logarithmic?

A

Exponential

41
Q

Whats the difference between quadratic notation and exponential notation?

A

quadratic == n^2
exponential == x^y