Phase 03 · DSA & Problem SolvingLesson 2 of 4
Time Complexity & Big-O
Measure how an algorithm scales before you write a single line.
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.
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-O | Name | Example | 1,000 items → roughly |
|---|---|---|---|
| O(1) | Constant | Look up d["key"] in a dict | 1 step |
| O(log n) | Logarithmic | Binary search in sorted data | ~10 steps |
| O(n) | Linear | Loop through a list once | 1,000 steps |
| O(n log n) | Linearithmic | Good sorting (merge sort, sorted()) | ~10,000 steps |
| O(n²) | Quadratic | Compare every pair (nested loops) | 1,000,000 steps |
| O(2ⁿ) | Exponential | Try every subset | astronomically many |
Reading Big-O from code#
# 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:
- Sequential steps add, and you keep the biggest: O(n) + O(n²) → O(n²).
- Nested loops multiply: a loop of n inside a loop of n → O(n²).
- Halving the problem each step gives O(log n).
- Drop constants: two separate passes, O(2n), is still O(n).
Binary search: why O(log n) is so powerful#
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
- Pick the middle item
mid = (lo + hi) // 2 - Equal to the target?return
mid - Smaller than the target?discard the left half
- 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.
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 stepRecap & next
Key takeaways
- Big-O describes how running time grows as input size
ngrows, ignoring constants. - Common classes, fastest to slowest: O(1), O(log n), O(n), O(n log n), O(n²), O(2ⁿ).
- Two nested loops over the same input usually mean O(n²).
- Halving the search space each step (like binary search) gives O(log n).
Quick check
2 questions · instant feedback
Go deeper
Finished this lesson?
Mark it complete to track your progress and keep your streak going.