Back to website
Mayank Aggarwal Learning Center

Phase 03 · DSA & Problem SolvingLesson 2 of 4

Time Complexity & Big-O

Measure how an algorithm scales before you write a single line.

  • 1:08:40 video
  • 3 min read
  • Intermediate
  • Deep dive
01

The problem

"Is my solution fast enough?" depends on how much data it will face. Code that runs instantly on 100 items can take hours on 10 million. Big-O notation is how engineers describe that growth, so you can predict performance before running anything.

02

The concept

Deep dive: 1h 9m video. You don’t need it all in one sitting. Watch the first 20 minutes for the core idea, read the write-up below, and come back for the rest when you need it.

What Big-O measures#

Big-O describes how the number of steps grows as the input size n grows. It ignores constants and small details, and focuses on the trend for large inputs.

The common classes#

Big-ONameExample1,000 items → roughly
O(1)ConstantLook up d["key"] in a dict1 step
O(log n)LogarithmicBinary search in sorted data~10 steps
O(n)LinearLoop through a list once1,000 steps
O(n log n)LinearithmicGood sorting (merge sort, sorted())~10,000 steps
O(n²)QuadraticCompare every pair (nested loops)1,000,000 steps
O(2ⁿ)ExponentialTry every subsetastronomically many

Reading Big-O from code#

Python
# O(1): one step regardless of size
first = items[0]

# O(n): one pass through the data
total = 0
for x in items:
    total += x

# O(n²): a loop inside a loop over the same data
for a in items:
    for b in items:
        if a + b == target:
            print(a, b)

Rules of thumb:

  1. Sequential steps add, and you keep the biggest: O(n) + O(n²) → O(n²).
  2. Nested loops multiply: a loop of n inside a loop of n → O(n²).
  3. Halving the problem each step gives O(log n).
  4. Drop constants: two separate passes, O(2n), is still O(n).

Binary search: why O(log n) is so powerful#

Python
def binary_search(sorted_items, target):
    lo, hi = 0, len(sorted_items) - 1
    while lo <= hi:
        mid = (lo + hi) // 2
        if sorted_items[mid] == target:
            return mid
        if sorted_items[mid] < target:
            lo = mid + 1      # discard the left half
        else:
            hi = mid - 1      # discard the right half
    return -1

Every step throws away half of what's left. One million sorted items need only about 20 comparisons.

One round of binary search

  1. Pick the middle itemmid = (lo + hi) // 2
  2. Equal to the target?return mid
  3. Smaller than the target?discard the left half
  4. Otherwisediscard the right half

Stops when the target is found, or returns -1 once lo > hi

Space complexity#

Big-O also describes memory. Creating a new list the same size as the input is O(n) extra space; using a few variables is O(1). Often you trade one for the other, for example using extra memory (a set) to save time.

03

Try it

Your task · about 10 minutes

For each snippet you wrote in the Python module, estimate its Big-O. Then find one you could improve.

Use the interactive visual above to explore it step by step
04

Recap & next

Key takeaways

  1. Big-O describes how running time grows as input size n grows, ignoring constants.
  2. Common classes, fastest to slowest: O(1), O(log n), O(n), O(n log n), O(n²), O(2ⁿ).
  3. Two nested loops over the same input usually mean O(n²).
  4. Halving the search space each step (like binary search) gives O(log n).

Quick check

2 questions · instant feedback

1Two nested loops each running n times are…
2What is the time complexity of binary search?

Finished this lesson?

Mark it complete to track your progress and keep your streak going.

Next