Lesson 1/25 Ā· š Complexity & Thinking
š Complexity & ThinkingLesson 1/25
Phase 0 Ā· Complexity & Thinking20 min
Big O Notation
The universal language for talking about algorithm efficiency
When you write code, there are always multiple ways to solve the same problem. Big O notation is how we measure which solution is *better*, not by timing it with a stopwatch, but by understanding how it scales.
The question Big O answers: "As the input gets huge, how does the work grow?"
The question Big O answers: "As the input gets huge, how does the work grow?"
The "Big" in Big O refers to the *dominant* term. We ignore constants and small terms because at scale, only the fastest-growing part matters. O(2n) ā O(n). O(n² + n) ā O(n²).
The Common Complexities (fastest ā slowest):
| Notation | Name | Example ||----------|------|---------|| O(1) | Constant | Array index lookup || O(log n) | Logarithmic | Binary search || O(n) | Linear | Loop through array || O(n log n) | Log-linear | Merge sort || O(n²) | Quadratic | Nested loops || O(2āæ) | Exponential | Recursive Fibonacci |
The difference is dramatic: with n=1,000,000 operations, O(log n) needs ~20 steps. O(n²) needs 1 trillion.
| Notation | Name | Example ||----------|------|---------|| O(1) | Constant | Array index lookup || O(log n) | Logarithmic | Binary search || O(n) | Linear | Loop through array || O(n log n) | Log-linear | Merge sort || O(n²) | Quadratic | Nested loops || O(2āæ) | Exponential | Recursive Fibonacci |
The difference is dramatic: with n=1,000,000 operations, O(log n) needs ~20 steps. O(n²) needs 1 trillion.
Spot the complexitypython
# O(1), constant, doesn't matter how big the list is
def get_first(items):
return items[0]
# O(n), linear, one pass through the data
def find_max(items):
max_val = items[0]
for item in items: # n iterations
if item > max_val:
max_val = item
return max_val
# O(n²), quadratic, nested loops
def has_duplicate(items):
for i in range(len(items)): # n iterations
for j in range(len(items)): # n iterations each
if i != j and items[i] == items[j]:
return True
return FalseNested loops almost always signal O(n²)
š¤Quick Check
You have a sorted array of 1,000,000 elements. Binary search finds an element in ~20 steps. What is its Big O?
š¤Quick Check
Which code is O(n²)?
Practice Exercises
0/2 solvedExercise 1 of 2easy
ā± 00:00Classify the Complexity
Analyze each function and print its Big O complexity.
Expected output:
Expected output:
find_item: O(n)sum_pairs: O(n^2)binary_lookup: O(log n)solution.py
1 / 2
Exercise 2 of 2medium
ā± 00:00Improve the Algorithm
This O(n²) duplicate checker is slow. Rewrite
Expected output:
has_duplicate_fast to run in O(n) using a set.Expected output:
Truesolution.py
2 / 2
Solve all 2 exercises to unlock completion