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

  • 35Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Big O Fundamentals
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

What is Big O notation?

Tap to flipClick to flipspace

A tool used to analyze algorithms and determine how much time they will take based on the approximate number of operations in the worst case, relative to input size.

Or swipe the card

35 cards

  1. What is Big O notation?

    A tool used to analyze algorithms and determine how much time they will take based on the approximate number of operations in the worst case, relative to input size.

  2. Why is Big O notation important for programmers?

    It allows comparison of different algorithms to determine which is more efficient, especially for large inputs, helping avoid writing code that becomes infeasible as input scales.

  3. What does 'n' represent in Big O notation?

    The size of the input to the algorithm.

  4. What two aspects do we focus on when analyzing time complexity with Big O?

    Large inputs (scaling) and worst-case scenario.

  5. What is the first step in finding the Big O notation of an algorithm?

    Figure out the approximate number of operations the algorithm takes as a function of input size n, called f(n).

  6. State the formal definition of Big O notation.

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

  7. In the Big O inequality f(n) ≤ c*g(n) for n ≥ N, what do we call g(n)?

    The Big O function (or the notation) that represents the time complexity.

  8. Why do we look for the smallest possible g(n) that satisfies the Big O inequality?

    We want the tightest upper bound. An arbitrarily large function (e.g., n^4) would always work, but it wouldn't be useful for comparison.

  9. Given f(n) = 2n + 1, prove that f(n) = O(n).

    Choose c = 3 and N = 1. Then for all n ≥ 1, 2n+1 ≤ 3n. Hence f(n) = O(n).

  10. Given f(n) = 2n² + n + 1, why can't g(n) = n work, but g(n) = n² works?

    Eventually n² outgrows any constant times n (e.g., for large n, 2n² dominates). With g(n)=n², choose a large c (e.g., 1000) and N=1 to satisfy 2n²+n+1 ≤ 1000n².

  11. What rule simplifies additive terms in Big O?

    Drop all terms except the largest (dominant) term; also drop constant coefficients.

  12. How do you simplify multiplicative terms in Big O?

    Keep all factors; you cannot drop a factor just because it's smaller than another factor.

  13. List the common Big O functions from fastest (best) to slowest (worst).

    O(1), O(log n), O(n), O(n log n), O(n²), O(2^n), O(n!)

  14. What does O(1) mean?

    Constant time: the algorithm takes the same number of steps regardless of input size.

  15. What does O(log n) mean and when does it typically occur?

    Logarithmic time: the number of operations increases slowly as input grows. Often occurs when the problem size is halved each step (e.g., binary search).

  16. What does O(n) mean?

    Linear time: the number of operations scales directly with the input size n.

  17. What does O(n²) indicate?

    Quadratic time: runtime grows proportionally to the square of the input size. Often from nested loops where each loop iterates over n.

  18. What is O(2^n) and why is it bad?

    Exponential time: runtime doubles (or more) with each additional input element. It becomes infeasible for even moderately large n.

  19. What is O(n!) and why is it the worst among common complexities?

    Factorial time: runtime grows as n factorial (n * (n-1) * ... * 1). Even n=20 yields 2.4e18 operations.

  20. Determine the Big O of a function that performs a constant number of operations regardless of input (e.g., checking if a number is odd using modulus).

    O(1)

  21. What is the Big O of summing all elements in a list of length n?

    O(n)

  22. What is the Big O of a nested loop where both loops iterate over the same input of size n (e.g., checking for duplicates)?

    O(n²)

  23. If an algorithm has two separate loops, one over input of size n and another over input of size m, what is the Big O?

    O(n + m)

  24. How do you handle a nested list (list of lists) with height h and width w? What is the Big O of iterating through all elements?

    O(h * w) or O(n²) if h and w are both proportional to n.

  25. What is the time complexity of the naive recursive Fibonacci algorithm?

    O(2^n)

  26. What is the time complexity of a recursive function that makes one recursive call with n/2 as input?

    O(log n) (base 2)

  27. How do you determine the Big O of a recursive function?

    Multiply the number of times the function runs (recursive calls) by the number of operations inside the function (excluding recursive calls).

  28. What Big O does a recursive function have if it makes three recursive calls in each step, each reducing n by a constant amount?

    O(3^n) (exponential, base 3)

  29. What is the Big O of the example 12 recursive function that calls itself n times with n-1? (Factorial recursion)

    O(n!)

  30. What is the time complexity of Python's max() function when called on a list of length n?

    O(n)

  31. What is the time complexity of sorting a list of length n using an efficient comparison-based sort (like Timsort)?

    O(n log n)

  32. What is the time complexity of popping from the front of a Python list?

    O(n) where n is the length of the list

  33. In example 8, strings in a list: what variables are used for time complexity and what is the final Big O?

    n = number of strings, k = maximum string length. Big O = O(n * k log k).

  34. In example 9, why is the time complexity O(n log n + m log m + (n+m)*k)?

    Sorting dictionary keys: n log n and m log m. The while loop processes each key multiple times: up to k times for the longest key, with each pop from front taking O(n+m) time worst-case, but the dominant term is (n+m)*k.

  35. What is the Big O of example 11: triple nested loops over the same input?

    O(n³)

Card 1 of 35
Card 1 of 35

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent