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

Speaker 1Speaker 2

Big O Notation Examples

5:18
0:00-5:18
  1. Michael

    Okay Hope, let's finally tackle big O notation. It's that tool programmers use to compare how efficient different algorithms are, especially when the input gets really large.

  2. Hope

    Right, and it's not about exact seconds — it's about how the number of operations grows as the input size grows. So if you're sorting a million numbers instead of ten, big O tells you which algorithm won't take forever.

  3. Michael

    Exactly. And we care about the worst case — that's the upper bound. If an algorithm sometimes finishes quickly but in the worst case takes a huge number of steps, big O captures that worst scenario.

  4. Hope

    So where do we start with actually finding it? Let's say I have a simple loop that sums numbers in a list.

  5. Michael

    You first count the operations. One operation to initialize a variable, then the loop runs n times — where n is the size of the input — and inside the loop you have two operations. So total operations f(n) = 2n + 1.

  6. Hope

    But we don't keep the 2 or the 1, right? We simplify down to just n. That's the big O — O(n).

  7. Michael

    Right, but it's worth seeing the math behind that simplification. The formal definition says f(n) = O(g(n)) if there exist constants c and N such that for all n >= N, f(n) <= c * g(n). So for f(n)=2n+1, we can pick c=3 and N=1, and then 2n+1 <= 3n holds.

  8. Hope

    So g(n) has to be a function that, when multiplied by some constant, eventually stays above f(n). That's why we pick the simplest g that works — in this case n — and we drop any smaller terms and constants because they become irrelevant for large n.

  9. Michael

    Exactly. For something like 2n² + n + 1, the n² term dominates as n grows, so big O is O(n²). You can't use just n because no matter what constant you multiply it by, n² will eventually be bigger.

  10. Hope

    Good. So the rule of thumb: keep the largest term, drop constants. If terms are added, the biggest one wins. If they're multiplied, you keep all factors.

  11. Michael

    Now let's talk about the common complexity classes. There's a chart you should know: O(1) constant time — a single operation regardless of input — O(log n) logarithmic, O(n) linear, O(n log n), O(n²), O(2ⁿ), and O(n!) — which is the worst.

  12. Hope

    Let's go through examples. First is example 0: checking if a number is odd or even. That's just a modulus operation — it's O(1), constant time, because it doesn't matter how big the number is.

  13. Michael

    Example 1: summing all numbers in a list — that's a loop over n elements, one operation per element, so O(n).

  14. Hope

    Example 2 has a nested loop. The outer loop runs n times, and for each iteration the inner loop runs n times. That's n * n = n² operations, so O(n²).

  15. Michael

    Example 3 introduces two separate inputs. You have a loop over list one and a loop over list two, but they're not nested. So total operations are n + m, and big O is O(n + m). You can't simplify further because n and m are independent.

  16. Hope

    Example 4: a nested list. If you have h outer lists and each inner list has w elements, and you loop through both, the total is h * w operations. That's O(h * w).

  17. Michael

    Example 5 is the Fibonacci sequence with recursion. Each call makes two more calls, and the recursion depth is n, so the tree has 2ⁿ nodes — that's O(2ⁿ). Terrible for performance.

  18. Hope

    Example 6 shows why you have to be careful with built-in functions. Finding the max of a list is O(n) because it has to check every element. So if you do that plus a loop over another list, you get O(n + m).

  19. Michael

    Example 7 is recursion that divides n by 2 each time. With one recursive call and the problem size halving, the number of steps is log₂(n). So O(log n).

  20. Hope

    Example 8 deals with a list of strings. The outer loop runs n times for each string, but inside you also loop through characters of each string — length k — and sometimes sort. Sorting is O(k log k). So total is O(n * k log k).

  21. Michael

    Example 9 is more complex: sorting two dictionaries, then a while loop that pops from the front — which is O(size of list) — and reinserts strings. The longest string length k multiplies the work, so final complexity is O(n log n + m log m + (n+m)*k).

  22. Hope

    Example 11 has three nested loops, each running n times — that's O(n³). And example 12 is factorial recursion: for input n, the function calls itself n times, each of those calls n-1 times, and so on. That's n! — O(n!).

  23. Michael

    So if you understand the first eight examples, you're in great shape. The later ones show how to handle more realistic code, but the core idea is always: count operations, drop constants, keep the dominant term, and use different variables for different inputs.

  24. Hope

    And that gives you the big O — a simple way to talk about how your algorithm will behave when the input gets huge. It's a superpower for writing efficient code.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent