Foundations
Big O Notation
Master Big O notation—the universal standard to classify algorithm efficiency, drop constants, and identify dominant growth terms.
Last Updated: July 31, 2026
•
15 min read
1. Introduction
What is Big O Notation?
Big O Notation is a mathematical syntax used in computer science to classify algorithms according to how their run time or space requirements grow as the input size (N) increases. The "O" stands for "Order of" (order of magnitude).
Why is it Important?
Big O allows software engineers to predict how code will behave at scale before shipping to production. It gives us a common vocabulary to discuss performance (e.g. "This approach isO(N log N) while the old one was O(N²)").
Where is it Used?
O(1) lookup vs Array O(N) lookup).2. Mental Model
Imagine transferring a file from New York to London.
3. Concept: The Three Rules of Big O
To simplify complexity calculations, computer scientists follow three fundamental rules:
Rule 1: Always Focus on Worst-Case Scenario
Big O measures the upper bound. If searching an element in an unsorted list of N items:Ω(1) (Omega)O(N) (Big O)We always design systems assuming the worst-case scenario (O(N)).
Rule 2: Drop Constants
When calculating operations like2N + 500:
O(2N + 500) → O(N)Rule 3: Keep Only the Dominant Term
If an algorithm takesN² + 100N + 50 operations:
N² = 100,000,000 (99.9% of the total work!)
- 100N = 1,000,000
100N and 50) become insignificant.O(N² + 100N + 50) → O(N²)4. Visuals
The Big O Hierarchy (Best to Worst)
Big O Complexity Reference Table
| Notation | Name | Common Example | Performance Rating |
|---|---|---|---|
O(1) | Constant | Array index access, HashMap lookup | 🟢 Excellent |
O(log N) | Logarithmic | Binary Search in sorted array | 🟢 Excellent |
O(N) | Linear | Single loop over array | 🟡 Fair |
O(N log N) | Linearithmic | Efficient sorting (MergeSort, QuickSort) | 🟡 Fair |
O(N²) | Quadratic | Nested loops (BubbleSort, brute force pair match) | 🔴 Poor |
O(2ⁿ) | Exponential | Recursive Fibonacci without memoization | 🔴 Horrible |
O(N!) | Factorial | Generating all permutations of a set | 🔴 Unusable for N > 12 |
5. Real-World Examples
O(1) Hash Table Lookup: Redis key-value cache lookups take O(1) time, retrieving user sessions in microseconds regardless of millions of active users.O(log N) Binary Search: Git bisect uses binary search over commit history to pinpoint which commit introduced a bug in 10 steps across 1,000 commits.O(N log N) Database Indexing: PostgreSQL creates B-Tree indexes on tables using O(N log N) sorting so subsequent queries run in O(log N).6. Interview Perspective
How Interviewers Ask This Topic
After writing code, the interviewer will ask: "What is the Big O time and space complexity of this code?"Common Mistakes
Warning: 1. Keeping Constants: Saying "
O(3N)" instead of simplifying to "O(N)".> 2. Confusing Different Inputs: If a function iterates over Array A (size N) and Array B (size M), the complexity isO(N + M), NOTO(N)orO(N²)!
> 3. Assuming Every Loop isO(N): If a loop runs up to a fixed constant (e.g.for (int i = 0; i < 100; i++)), it isO(1), notO(N).
Important Interviewer Tips
7. Summary
O(50N) \rightarrow O(N).O(N² + N) \rightarrow O(N²).O(A + B) or O(A × B).