Big O Notation Examples
- 18Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What does Big O notation primarily measure?
What does Big O notation primarily measure?
18 questions
- 1
What does Big O notation primarily measure?
- A
The approximate number of operations in the worst case for large inputs
- B
The exact number of seconds an algorithm takes to run
- C
The number of lines of code in an algorithm
- D
The average number of operations for any input size
Correct answer
AThe approximate number of operations in the worst case for large inputs
Explanation
Big O notation analyzes algorithms by estimating the number of operations in the worst-case scenario, focusing on behavior as the input size grows large.
- A
- 2
In the formal definition of Big O, what does the constant c represent?
- A
The exact factor by which f(n) exceeds g(n)
- B
A constant multiplier that makes c⋅g(n) an upper bound for f(n) for large n
- C
The base of the logarithm in the complexity
- D
The smallest input size where the inequality holds
Correct answer
BA constant multiplier that makes c⋅g(n) an upper bound for f(n) for large n
Explanation
The definition requires f(n)≤c⋅g(n) for all n≥n0, where c is a constant that ensures g(n) upper-bounds f(n).
- A
- 3
Which of the following is the correct simplification of f(n)=3n2+2n+5?
- A
O(3n2)
- B
O(n2)
- C
O(3n2+2n)
- D
O(n)
Correct answer
BO(n2)
Explanation
We drop constants and lower-order terms; the dominant term is n2, so the complexity is O(n2).
- A
- 4
When analyzing two separate loops that are not nested, one iterating over n elements and the other over m elements, the overall time complexity is:
- A
O(n+m)
- B
O(n⋅m)
- C
O(max(n,m))
- D
O(n) if n>m, else O(m)
Correct answer
AO(n+m)
Explanation
Since the loops run sequentially, the total operations are n+m, giving O(n+m).
- A
- 5
A function contains a single loop that runs n times, and inside the loop there is another loop that also runs n times independent of the outer index. What is the time complexity?
- A
O(2n)
- B
O(n)
- C
O(logn)
- D
O(n2)
Correct answer
DO(n2)
Explanation
The inner loop runs n times for each of n outer iterations, resulting in n×n=n2 operations, so O(n2).
- A
- 6
What is the time complexity of a recursive algorithm that makes two recursive calls, each on a problem of size n−1, and no other significant work?
- A
O(logn)
- B
O(n)
- C
O(n2)
- D
O(2n)
Correct answer
DO(2n)
Explanation
Each call spawns two more calls, and the depth is n, leading to approximately 2n calls, so O(2n).
- A
- 7
If a recursive function reduces the input size by half at each step and makes only one recursive call, the time complexity is:
- A
O(nlogn)
- B
O(logn)
- C
O(2n)
- D
O(n)
Correct answer
BO(logn)
Explanation
The number of steps is about log2n because the input is halved each time, so O(logn).
- A
- 8
What is the time complexity of the Fibonacci sequence implemented with simple recursion as shown?
- A
O(2n)
- B
O(logn)
- C
O(n)
- D
O(n2)
Correct answer
AO(2n)
Explanation
Each call makes two recursive calls, and the recursion tree height is n, resulting in O(2n) operations.
- A
- 9
In the video, what is the time complexity of a function that checks if a number is odd or even using modulus?
- A
O(n)
- B
O(logn)
- C
O(n2)
- D
O(1)
Correct answer
DO(1)
Explanation
The operation runs in constant time regardless of input size, so O(1).
- A
- 10
A function computes the sum of all elements in a list of length n. What is its time complexity?
- A
O(n2)
- B
O(n)
- C
O(1)
- D
O(logn)
Correct answer
BO(n)
Explanation
It iterates through all n elements once, performing constant work per element, so O(n).
- A
- 11
What is the time complexity of the built-in
max()function on a list of n elements?- A
O(1)
- B
O(logn)
- C
O(n)
- D
O(nlogn)
Correct answer
CO(n)
Explanation
max()must examine every element to find the maximum, so it runs in O(n) time. - A
- 12
What is the time complexity of
list.pop(0)in Python?- A
O(n2)
- B
O(logn)
- C
O(n)
- D
O(1)
Correct answer
CO(n)
Explanation
Popping the first element requires shifting all remaining elements, which is O(n).
- A
- 13
The most efficient comparison-based sorting algorithm has a worst-case time complexity of:
- A
O(logn)
- B
O(nlogn)
- C
O(n)
- D
O(n2)
Correct answer
BO(nlogn)
Explanation
Comparison sorts like merge sort have O(nlogn) worst-case time complexity.
- A
- 14
A function processes a nested list with h rows and w columns using two nested loops (one for rows, one for columns). What is the time complexity?
- A
O(max(h,w))
- B
O(h⋅w)
- C
O(h+w)
- D
O(h2)
Correct answer
BO(h⋅w)
Explanation
The outer loop runs h times, and for each row, the inner loop runs w times, giving h⋅w operations.
- A
- 15
When analyzing code that includes a sort operation on a string of length k, what is the time contribution of sorting?
- A
O(klogk)
- B
O(k2)
- C
O(logk)
- D
O(k)
Correct answer
AO(klogk)
Explanation
Sorting a sequence of length k using an efficient comparison sort is O(klogk).
- A
- 16
What is the time complexity of the factorial recursion example (Example 12) shown in the video?
- A
O(nlogn)
- B
O(2n)
- C
O(n2)
- D
O(n!)
Correct answer
DO(n!)
Explanation
Each call makes n recursive calls to n−1, leading to n! total operations, giving O(n!).
- A
- 17
In Example 11, a triple-nested loop structure is analyzed. What is the resulting time complexity?
- A
O(2n)
- B
O(n)
- C
O(n3)
- D
O(n2)
Correct answer
CO(n3)
Explanation
Three nested loops each running n times result in n×n×n=n3 operations, so O(n3).
- A
- 18
Which of the following is NOT a valid simplification rule for Big O?
- A
Drop constant multipliers
- B
Drop all logarithmic factors because they are constant
- C
Drop lower-order additive terms
- D
Keep all factors when multiplying different variables
Correct answer
BDrop all logarithmic factors because they are constant
Explanation
Logarithmic factors are not constant; they grow slowly but are still part of the complexity (e.g., O(nlogn) is valid).
- A

Ask anything
We've read this whole study set, so questions about it get the best answers.