1. Home
  2. Explore
  3. Computer Science
  4. Big O Notation Examples

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Big O Notation Examples

  • 26Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Big O Basics

What is Big O notation and what does it measure?

26 questions

  1. 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 nnn.

    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. 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. 3

    What does the variable nnn typically represent in Big O analysis?

    Model answer

    nnn 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 nnn.

  4. 4

    Explain the mathematical definition of f(n)=O(g(n))f(n) = O(g(n))f(n)=O(g(n)).

    Model answer

    f(n)=O(g(n))f(n) = O(g(n))f(n)=O(g(n)) if there exist constants c≥1c \geq 1c≥1 and N≥1N \geq 1N≥1 such that for all n≥Nn \geq Nn≥N, f(n)≤c⋅g(n)f(n) \leq c \cdot g(n)f(n)≤c⋅g(n).

    Explanation

    This means g(n)g(n)g(n) is an upper bound on f(n)f(n)f(n) for large nnn, up to a constant factor.

  5. 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)2n+1 = O(n)2n+1=O(n) and 2n2+n+1=O(n2)2n^2 + n + 1 = O(n^2)2n2+n+1=O(n2).

    Explanation

    Since Big O focuses on large nnn, constants and smaller terms become negligible compared to the dominant term.

  6. 6

    What is the time complexity of an algorithm that performs a constant number of operations regardless of input size?

    Model answer

    O(1)O(1)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. 7

    Describe logarithmic time complexity O(log⁡n)O(\log n)O(logn) and give an example of how it arises.

    Model answer

    An algorithm has O(log⁡n)O(\log n)O(logn) complexity when it reduces the problem size by a constant factor each step, such as a recursive function that divides nnn by 2 each call.

    Explanation

    For instance, a function that calls itself on n/2n/2n/2 only once will have at most log⁡2n\log_2 nlog2​n recursive calls.

  8. 8

    What does O(n)O(n)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 nnn times with constant work inside is O(n)O(n)O(n).

  9. 9

    What is the time complexity of an efficient comparison-based sorting algorithm?

    Model answer

    O(nlog⁡n)O(n \log n)O(nlogn).

    Explanation

    This is typical for algorithms like mergesort or heapsort, where the input is divided and compared.

  10. 10

    Why is O(n2)O(n^2)O(n2) considered poor performance?

    Model answer

    Because runtime grows quadratically with input; for n=100n=100n=100, it performs 10,000 operations, which becomes massive for large nnn.

    Explanation

    Nested loops where each loop runs nnn times typically produce O(n2)O(n^2)O(n2).

  11. 11

    What is exponential time complexity O(2n)O(2^n)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. 12

    What is factorial time complexity O(n!)O(n!)O(n!) and how does it arise?

    Model answer

    Factorial complexity means the number of operations grows as n×(n−1)×(n−2)×⋯×1n \times (n-1) \times (n-2) \times \dots \times 1n×(n−1)×(n−2)×⋯×1, often from recursive algorithms that make nnn recursive calls, then (n−1)(n-1)(n−1), and so on.

    Explanation

    An example is the recursive function in Example 12, which calls itself nnn times with n−1n-1n−1, leading to a factorial number of base cases.

  13. 13

    What is the time complexity of a single loop that runs nnn times with constant operations inside?

    Model answer

    O(n)O(n)O(n).

    Explanation

    The loop executes nnn times, each iteration taking constant time, so total operations are proportional to nnn.

  14. 14

    What is the time complexity of two nested loops, each iterating nnn times?

    Model answer

    O(n2)O(n^2)O(n2).

    Explanation

    The inner loop runs nnn times for each of the nnn iterations of the outer loop, resulting in n2n^2n2 operations.

  15. 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 nnn times and another runs mmm times, the total is O(n+m)O(n+m)O(n+m).

    Explanation

    Since they execute sequentially, the total operations are the sum of the separate loop operations.

  16. 16

    What is the time complexity of a loop with nnn iterations that contains another loop that runs mmm times?

    Model answer

    O(n×m)O(n \times m)O(n×m).

    Explanation

    The inner loop runs mmm times for each of the nnn outer iterations, giving n⋅mn \cdot mn⋅m total operations.

  17. 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. 18

    Why does the naive Fibonacci recursion have O(2n)O(2^n)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 2n2^n2n calls.

    Explanation

    For n=5n=5n=5, the tree has 2 calls at level 1, 4 at level 2, etc., totaling roughly 2n2^n2n calls.

  19. 19

    What is the time complexity of a recursive function that calls itself once on n/2n/2n/2 and does constant work?

    Model answer

    O(log⁡n)O(\log n)O(logn).

    Explanation

    Since the input size halves each call, the number of recursive calls is log⁡2n\log_2 nlog2​n, and each call does constant work.

  20. 20

    Why does the recursion in Example 12 (factorial) yield O(n!)O(n!)O(n!)?

    Model answer

    Because for input nnn, the function makes nnn recursive calls with n−1n-1n−1, then each of those makes (n−1)(n-1)(n−1) calls, and so on, leading to a total number of calls proportional to n!n!n!.

    Explanation

    The number of base cases equals n!n!n!, and total calls are at most 2⋅n!2 \cdot n!2⋅n!, which simplifies to O(n!)O(n!)O(n!).

  21. 21

    What is the time complexity of the built-in max() function on a list of size nnn?

    Model answer

    O(n)O(n)O(n).

    Explanation

    max() must examine every element to find the largest, so it takes nnn operations in the worst case.

  22. 22

    What is the time complexity of sorting a list of size nnn using an efficient comparison-based sort?

    Model answer

    O(nlog⁡n)O(n \log n)O(nlogn).

    Explanation

    Algorithms like mergesort or Python's Timsort run in O(nlog⁡n)O(n \log n)O(nlogn) time.

  23. 23

    Why is pop(0) on a Python list an O(n)O(n)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 nnn elements, so the cost is linear in the list size.

  24. 24

    When analyzing code that processes a list of strings, what additional variable is introduced to account for string length?

    Model answer

    kkk, the maximum length of any string in the list.

    Explanation

    Strings themselves have lengths that affect operations like character iteration or sorting; kkk captures the worst-case length.

  25. 25

    How do you determine the time complexity for nested loops over a two-dimensional list where the outer list has hhh elements and each inner list has www elements?

    Model answer

    The complexity is O(h×w)O(h \times w)O(h×w).

    Explanation

    If the outer loop runs hhh times and the inner loop runs www times, total operations are h×wh \times wh×w.

  26. 26

    When an algorithm has multiple inputs of different sizes, how are they represented in Big O?

    Model answer

    Use separate variables, such as nnn and mmm, for each input's size.

    Explanation

    For example, if two lists of sizes nnn and mmm are processed independently, the complexity may be O(n)+O(m)O(n) + O(m)O(n)+O(m) or O(n×m)O(n \times m)O(n×m) depending on nesting.

1 of 26
Question 1 of 26

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent