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

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

What does Big O notation primarily measure?

18 questions

  1. 1

    What does Big O notation primarily measure?

    1. A

      The approximate number of operations in the worst case for large inputs

    2. B

      The exact number of seconds an algorithm takes to run

    3. C

      The number of lines of code in an algorithm

    4. D

      The average number of operations for any input size

    Correct answer
    A

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

  2. 2

    In the formal definition of Big O, what does the constant ccc represent?

    1. A

      The exact factor by which f(n)f(n)f(n) exceeds g(n)g(n)g(n)

    2. B

      A constant multiplier that makes c⋅g(n)c \cdot g(n)c⋅g(n) an upper bound for f(n)f(n)f(n) for large nnn

    3. C

      The base of the logarithm in the complexity

    4. D

      The smallest input size where the inequality holds

    Correct answer
    B

    A constant multiplier that makes c⋅g(n)c \cdot g(n)c⋅g(n) an upper bound for f(n)f(n)f(n) for large nnn

    Explanation

    The definition requires f(n)≤c⋅g(n)f(n) \leq c \cdot g(n)f(n)≤c⋅g(n) for all n≥n0n \geq n_0n≥n0​, where ccc is a constant that ensures g(n)g(n)g(n) upper-bounds f(n)f(n)f(n).

  3. 3

    Which of the following is the correct simplification of f(n)=3n2+2n+5f(n) = 3n^2 + 2n + 5f(n)=3n2+2n+5?

    1. A

      O(3n2)O(3n^2)O(3n2)

    2. B

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

    3. C

      O(3n2+2n)O(3n^2 + 2n)O(3n2+2n)

    4. D

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

    Correct answer
    B

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

    Explanation

    We drop constants and lower-order terms; the dominant term is n2n^2n2, so the complexity is O(n2)O(n^2)O(n2).

  4. 4

    When analyzing two separate loops that are not nested, one iterating over nnn elements and the other over mmm elements, the overall time complexity is:

    1. A

      O(n+m)O(n + m)O(n+m)

    2. B

      O(n⋅m)O(n \cdot m)O(n⋅m)

    3. C

      O(max⁡(n,m))O(\max(n, m))O(max(n,m))

    4. D

      O(n)O(n)O(n) if n>mn > mn>m, else O(m)O(m)O(m)

    Correct answer
    A

    O(n+m)O(n + m)O(n+m)

    Explanation

    Since the loops run sequentially, the total operations are n+mn + mn+m, giving O(n+m)O(n + m)O(n+m).

  5. 5

    A function contains a single loop that runs nnn times, and inside the loop there is another loop that also runs nnn times independent of the outer index. What is the time complexity?

    1. A

      O(2n)O(2^n)O(2n)

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    D

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

    Explanation

    The inner loop runs nnn times for each of nnn outer iterations, resulting in n×n=n2n \times n = n^2n×n=n2 operations, so O(n2)O(n^2)O(n2).

  6. 6

    What is the time complexity of a recursive algorithm that makes two recursive calls, each on a problem of size n−1n-1n−1, and no other significant work?

    1. A

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

    2. B

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

    3. C

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

    4. D

      O(2n)O(2^n)O(2n)

    Correct answer
    D

    O(2n)O(2^n)O(2n)

    Explanation

    Each call spawns two more calls, and the depth is nnn, leading to approximately 2n2^n2n calls, so O(2n)O(2^n)O(2n).

  7. 7

    If a recursive function reduces the input size by half at each step and makes only one recursive call, the time complexity is:

    1. A

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

    2. B

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

    3. C

      O(2n)O(2^n)O(2n)

    4. D

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

    Correct answer
    B

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

    Explanation

    The number of steps is about log⁡2n\log_2 nlog2​n because the input is halved each time, so O(log⁡n)O(\log n)O(logn).

  8. 8

    What is the time complexity of the Fibonacci sequence implemented with simple recursion as shown?

    1. A

      O(2n)O(2^n)O(2n)

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    A

    O(2n)O(2^n)O(2n)

    Explanation

    Each call makes two recursive calls, and the recursion tree height is nnn, resulting in O(2n)O(2^n)O(2n) operations.

  9. 9

    In the video, what is the time complexity of a function that checks if a number is odd or even using modulus?

    1. A

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

    2. B

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

    3. C

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

    4. D

      O(1)O(1)O(1)

    Correct answer
    D

    O(1)O(1)O(1)

    Explanation

    The operation runs in constant time regardless of input size, so O(1)O(1)O(1).

  10. 10

    A function computes the sum of all elements in a list of length nnn. What is its time complexity?

    1. A

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

    2. B

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

    3. C

      O(1)O(1)O(1)

    4. D

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

    Correct answer
    B

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

    Explanation

    It iterates through all nnn elements once, performing constant work per element, so O(n)O(n)O(n).

  11. 11

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

    1. A

      O(1)O(1)O(1)

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    C

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

    Explanation

    max() must examine every element to find the maximum, so it runs in O(n)O(n)O(n) time.

  12. 12

    What is the time complexity of list.pop(0) in Python?

    1. A

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

    2. B

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

    3. C

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

    4. D

      O(1)O(1)O(1)

    Correct answer
    C

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

    Explanation

    Popping the first element requires shifting all remaining elements, which is O(n)O(n)O(n).

  13. 13

    The most efficient comparison-based sorting algorithm has a worst-case time complexity of:

    1. A

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

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    B

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

    Explanation

    Comparison sorts like merge sort have O(nlog⁡n)O(n \log n)O(nlogn) worst-case time complexity.

  14. 14

    A function processes a nested list with hhh rows and www columns using two nested loops (one for rows, one for columns). What is the time complexity?

    1. A

      O(max⁡(h,w))O(\max(h, w))O(max(h,w))

    2. B

      O(h⋅w)O(h \cdot w)O(h⋅w)

    3. C

      O(h+w)O(h + w)O(h+w)

    4. D

      O(h2)O(h^2)O(h2)

    Correct answer
    B

    O(h⋅w)O(h \cdot w)O(h⋅w)

    Explanation

    The outer loop runs hhh times, and for each row, the inner loop runs www times, giving h⋅wh \cdot wh⋅w operations.

  15. 15

    When analyzing code that includes a sort operation on a string of length kkk, what is the time contribution of sorting?

    1. A

      O(klog⁡k)O(k \log k)O(klogk)

    2. B

      O(k2)O(k^2)O(k2)

    3. C

      O(log⁡k)O(\log k)O(logk)

    4. D

      O(k)O(k)O(k)

    Correct answer
    A

    O(klog⁡k)O(k \log k)O(klogk)

    Explanation

    Sorting a sequence of length kkk using an efficient comparison sort is O(klog⁡k)O(k \log k)O(klogk).

  16. 16

    What is the time complexity of the factorial recursion example (Example 12) shown in the video?

    1. A

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

    2. B

      O(2n)O(2^n)O(2n)

    3. C

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

    4. D

      O(n!)O(n!)O(n!)

    Correct answer
    D

    O(n!)O(n!)O(n!)

    Explanation

    Each call makes nnn recursive calls to n−1n-1n−1, leading to n!n!n! total operations, giving O(n!)O(n!)O(n!).

  17. 17

    In Example 11, a triple-nested loop structure is analyzed. What is the resulting time complexity?

    1. A

      O(2n)O(2^n)O(2n)

    2. B

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

    3. C

      O(n3)O(n^3)O(n3)

    4. D

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

    Correct answer
    C

    O(n3)O(n^3)O(n3)

    Explanation

    Three nested loops each running nnn times result in n×n×n=n3n \times n \times n = n^3n×n×n=n3 operations, so O(n3)O(n^3)O(n3).

  18. 18

    Which of the following is NOT a valid simplification rule for Big O?

    1. A

      Drop constant multipliers

    2. B

      Drop all logarithmic factors because they are constant

    3. C

      Drop lower-order additive terms

    4. D

      Keep all factors when multiplying different variables

    Correct answer
    B

    Drop 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(nlog⁡n)O(n \log n)O(nlogn) is valid).

Question 1 of 18
Question 1 of 18

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent