Module 2 Lesson 10: Time Complexity Basics (Big O)
·Programming

Module 2 Lesson 10: Time Complexity Basics (Big O)

Learn how to measure the efficiency of your code. Understand Big O notation and why it matters for scaling software.

Module 2 Lesson 10: Time Complexity Basics

Why did we learn that Binary Search is "better" than Linear Search? Why do we care if an algorithm is "fast"? In this lesson, we introduce Time Complexity and Big O Notation—the mathematical way programmers talk about the efficiency of their code.

Lesson Overview

In this lesson, we will cover:

  • The Big Question: How do we measure speed?
  • Big O Notation: The universal language of performance.
  • O(1): Constant Time.
  • O(n): Linear Time.
  • O(log n): Logarithmic Time.
  • Best vs. Worst Case: Why we plan for the worst.

1. How Do We Measure Speed?

We don't use a stopwatch. Why? Because the same code will run at different speeds on a supercomputer vs. a 10-year-old laptop. Instead, we measure how the number of operations grows as the input data grows.


2. Meet Big O Notation

Big O is a way to group algorithms by their "growth rate."

O(1) - Constant Time

The speed is the same regardless of the data size.

  • Example: Looking up an item in a Dictionary by its key.
  • Analogy: Grabbing a book off your nightstand. It takes the same time no matter how many books are in your library.

O(n) - Linear Time

The time grows exactly the same as the data size.

  • Example: Linear Search. If you have 10 items, it takes 10 steps. If you have 10,000 items, it takes 10,000 steps.
  • Analogy: Reading every page in a book to find a specific word.

O(log n) - Logarithmic Time

The time grows very slowly compared to the data.

  • Example: Binary Search. Every step cuts the work in half.
  • Analogy: Finding a name in a phone book by opening to the middle repeatedly.

3. Why It Matters

Imagine your app starts with 100 users and grows to 1,000,000 users.

  • An O(n) algorithm will take 10,000 times longer.
  • An O(log n) algorithm will only take a few more steps.

Efficiency is the secret to building software that scales!


4. Best vs. Worst Case

Usually, we talk about the Worst Case. If you are doing a Linear Search for "Alex":

  • Best Case: Alex is the first item (1 step).
  • Worst Case: Alex is the last item (N steps).
  • Programmers always plan for the worst case to ensure the app never freezes unexpectedly.

Practice Exercise: The Big O Identity

Look at the following Python snippets and identify their Big O complexity:

  1. Printing the first item of a list: print(my_list[0])
  2. Printing every item in a list using a for loop.
  3. Looking up a value in a dictionary: print(my_dict["key"])
  4. Checking if an item exists in a Set: if item in my_set:

Quick Knowledge Check

  1. Why don't we use a stopwatch to measure algorithm speed?
  2. What does the n represent in O(n)?
  3. Which is faster as data grows: O(n) or O(log n)?
  4. What is the Time Complexity of looking up a key in a Dictionary?

Key Takeaways

  • Time complexity measures how code scales with more data.
  • Big O notation is the standard way to express this scaling.
  • O(1) and O(log n) are the "Gold Standards" of performance.
  • Always consider the worst-case scenario when designing logic.

What’s Next?

We’ve finished the theoretical part of Module 2. Now it's time to apply it! In Lesson 11, we’ll build several Hands-on Projects like a Contact Book and a Grade Tracker to see these structures and algorithms in action!

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn