Foundations
Time Complexity
Learn how to measure and compare algorithm execution time as input size grows, without relying on clock speed.
Last Updated: July 31, 2026
•
15 min read
1. Introduction
What is Time Complexity?
Time Complexity is a way to describe how the execution time of a program changes as the input size grows larger. It counts the number of fundamental operations (like comparisons, additions, or assignments) an algorithm performs.Why is it Important?
Measuring time in seconds is unreliable because a fast laptop runs code quicker than an older phone. Time complexity provides a universal, hardware-independent metric to compare which algorithm is truly more efficient.Where is it Used?
2. Mental Model
Imagine you want to find a contact's phone number.
Time complexity focuses on how the workload scales when input increases tenfold or a millionfold.
3. Concept: Counting Operations
Instead of using a stopwatch, computer scientists measure efficiency by counting basic operations.
Example 1: Constant Operations (Independent of N)
Looking up an item by index in an array:This is Constant Time—the work stays the same no matter how big N gets.
Example 2: Linear Operations (Grows with N)
Checking every element in a list:This is Linear Time—if input size doubles, the work doubles.
Example 3: Quadratic Operations (Grows with N²)
Comparing every element with every other element (nested loops):This is Quadratic Time—small increases in input lead to massive explosions in total operations.
4. Visuals
Growth of Operations as Input (N) Increases
| Input Size (N) | Constant O(1) | Logarithmic O(log N) | Linear O(N) | Quadratic O(N²) |
|---|---|---|---|---|
| 10 | 1 operation | ~3 operations | 10 operations | 100 operations |
| 100 | 1 operation | ~7 operations | 100 operations | 10,000 operations |
| 1,000 | 1 operation | ~10 operations | 1,000 operations | 1,000,000 operations |
| 1,000,000 | 1 operation | ~20 operations | 1,000,000 operations | 1,000,000,000,000 operations (CPU hangs!) |
5. Real-World Examples
6. Interview Perspective
How Interviewers Ask This Topic
After you propose a solution, interviewers will ask: "What is the time complexity of your approach?"Common Mistakes
Warning: 1. Confusing Clock Seconds with Time Complexity: Saying "This algorithm takes 2 milliseconds" instead of analyzing operation growth
O(N).> 2. Ignoring Hidden Loops: Calling built-in functions likeindexOf(),contains(), or string concatenation inside a loop without realizing they add an innerO(N)factor.
> 3. Counting Non-Dominant Operations: Worrying about2N + 5operations instead of focusing on the dominant growth rateO(N).
Important Interviewer Tips
N = number of elements, M = string length).7. Summary
N increases.O(N²).O(log N).