Big O Notation Examples
- 26Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is Big O notation and what does it measure?
26 questions
- 1
What is Big O notation and what does it measure?
Model answer
Big O notation is a tool used to analyze algorithms by estimating the number of operations they perform in the worst case, expressed as a function of the input size n.
Explanation
It measures the growth rate of an algorithm's runtime as the input size increases, focusing on large inputs and ignoring constants and smaller terms.
- 2
Why is Big O notation important for programmers?
Model answer
It allows comparison of algorithm efficiency, helping programmers choose the most efficient solution that will scale well for large inputs.
Explanation
Without it, code may work for small inputs but become infeasible for larger ones due to excessive runtime.
- 3
What does the variable n typically represent in Big O analysis?
Model answer
n represents the size of the input to the algorithm.
Explanation
It is a placeholder for the input size, and all operations are counted relative to n.
- 4
Explain the mathematical definition of f(n)=O(g(n)).
Model answer
f(n)=O(g(n)) if there exist constants c≥1 and N≥1 such that for all n≥N, f(n)≤c⋅g(n).
Explanation
This means g(n) is an upper bound on f(n) for large n, up to a constant factor.
- 5
What is the rule for dropping constants and lower-order terms when determining Big O?
Model answer
In additive expressions, keep only the term with the highest growth rate and drop all constants; e.g., 2n+1=O(n) and 2n2+n+1=O(n2).
Explanation
Since Big O focuses on large n, constants and smaller terms become negligible compared to the dominant term.
- 6
What is the time complexity of an algorithm that performs a constant number of operations regardless of input size?
Model answer
O(1) (constant time).
Explanation
For example, checking if a number is odd or even using modulo takes the same number of steps for any input.
- 7
Describe logarithmic time complexity O(logn) and give an example of how it arises.
Model answer
An algorithm has O(logn) complexity when it reduces the problem size by a constant factor each step, such as a recursive function that divides n by 2 each call.
Explanation
For instance, a function that calls itself on n/2 only once will have at most log2n recursive calls.
- 8
What does O(n) mean in terms of runtime growth?
Model answer
Runtime grows linearly with the input size: if input doubles, runtime roughly doubles.
Explanation
For example, a single loop that iterates n times with constant work inside is O(n).
- 9
What is the time complexity of an efficient comparison-based sorting algorithm?
Model answer
O(nlogn).
Explanation
This is typical for algorithms like mergesort or heapsort, where the input is divided and compared.
- 10
Why is O(n2) considered poor performance?
Model answer
Because runtime grows quadratically with input; for n=100, it performs 10,000 operations, which becomes massive for large n.
Explanation
Nested loops where each loop runs n times typically produce O(n2).
- 11
What is exponential time complexity O(2n) and when does it occur?
Model answer
Exponential complexity means runtime doubles with each additional input element, often occurring in recursive algorithms that make two recursive calls per call with a constant reduction.
Explanation
The naive Fibonacci recursion is a classic example, where the call tree grows exponentially.
- 12
What is factorial time complexity O(n!) and how does it arise?
Model answer
Factorial complexity means the number of operations grows as n×(n−1)×(n−2)×⋯×1, often from recursive algorithms that make n recursive calls, then (n−1), and so on.
Explanation
An example is the recursive function in Example 12, which calls itself n times with n−1, leading to a factorial number of base cases.
- 13
What is the time complexity of a single loop that runs n times with constant operations inside?
Model answer
O(n).
Explanation
The loop executes n times, each iteration taking constant time, so total operations are proportional to n.
- 14
What is the time complexity of two nested loops, each iterating n times?
Model answer
O(n2).
Explanation
The inner loop runs n times for each of the n iterations of the outer loop, resulting in n2 operations.
- 15
How do you compute the Big O of code with two independent loops that do not nest?
Model answer
Add their complexities: if one loop runs n times and another runs m times, the total is O(n+m).
Explanation
Since they execute sequentially, the total operations are the sum of the separate loop operations.
- 16
What is the time complexity of a loop with n iterations that contains another loop that runs m times?
Model answer
O(n×m).
Explanation
The inner loop runs m times for each of the n outer iterations, giving n⋅m total operations.
- 17
How do you analyze a recursive algorithm's Big O?
Model answer
Determine the number of recursive calls multiplied by the work per call (excluding the recursive calls themselves).
Explanation
The total time is the number of times the function is called times the constant work inside the function, ignoring the recursive calls themselves.
- 18
Why does the naive Fibonacci recursion have O(2n) time complexity?
Model answer
Each call makes two recursive calls with input reduced by a constant amount, creating an exponential call tree with about 2n calls.
Explanation
For n=5, the tree has 2 calls at level 1, 4 at level 2, etc., totaling roughly 2n calls.
- 19
What is the time complexity of a recursive function that calls itself once on n/2 and does constant work?
Model answer
O(logn).
Explanation
Since the input size halves each call, the number of recursive calls is log2n, and each call does constant work.
- 20
Why does the recursion in Example 12 (factorial) yield O(n!)?
Model answer
Because for input n, the function makes n recursive calls with n−1, then each of those makes (n−1) calls, and so on, leading to a total number of calls proportional to n!.
Explanation
The number of base cases equals n!, and total calls are at most 2⋅n!, which simplifies to O(n!).
- 21
What is the time complexity of the built-in
max()function on a list of size n?Model answer
O(n).
Explanation
max()must examine every element to find the largest, so it takes n operations in the worst case. - 22
What is the time complexity of sorting a list of size n using an efficient comparison-based sort?
Model answer
O(nlogn).
Explanation
Algorithms like mergesort or Python's Timsort run in O(nlogn) time.
- 23
Why is
pop(0)on a Python list an O(n) operation?Model answer
Because removing the first element requires shifting all remaining elements one position to the left, taking time proportional to the list length.
Explanation
The shift operation affects n elements, so the cost is linear in the list size.
- 24
When analyzing code that processes a list of strings, what additional variable is introduced to account for string length?
Model answer
k, the maximum length of any string in the list.
Explanation
Strings themselves have lengths that affect operations like character iteration or sorting; k captures the worst-case length.
- 25
How do you determine the time complexity for nested loops over a two-dimensional list where the outer list has h elements and each inner list has w elements?
Model answer
The complexity is O(h×w).
Explanation
If the outer loop runs h times and the inner loop runs w times, total operations are h×w.
- 26
When an algorithm has multiple inputs of different sizes, how are they represented in Big O?
Model answer
Use separate variables, such as n and m, for each input's size.
Explanation
For example, if two lists of sizes n and m are processed independently, the complexity may be O(n)+O(m) or O(n×m) depending on nesting.

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