Complexity
How to measure how fast your code runs and how much memory it uses — the skill that separates good code from great code.
Quick answer
Time complexity describes how the number of steps grows as the input size n grows, and space complexity how the extra memory grows. Big O names the growth rate: O(1) constant, O(log n), O(n), O(n log n), O(n²), O(2ⁿ).
- Big O ignores constants and keeps the fastest-growing term.
- One loop over n items is O(n); a loop inside a loop is O(n²).
- Halving the problem each step gives O(log n).
#include <stdio.h>
int main(void) {
for (int n = 10; n <= 1000; n *= 10) {
long linear = 0, quadratic = 0;
for (int i = 0; i < n; i++) linear++;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) quadratic++;
printf("n=%4d O(n)=%4ld O(n^2)=%7ld\n", n, linear, quadratic);
}
return 0;
}Output
n= 10 O(n)= 10 O(n^2)= 100 n= 100 O(n)= 100 O(n^2)= 10000 n=1000 O(n)=1000 O(n^2)=1000000
Two programs can both solve the same problem and produce the same correct answer — yet one can be a million times faster than the other. How do you know which one is better? How do you even measure it?
That is exactly what this lesson answers. Complexity is the scoreboard for code. It tells you — before you even run the program — how it will behave when the data gets big. This is what every interviewer means when they ask "what is the time complexity of your solution?"
What is Time Complexity?
Time complexity does not measure how many seconds your code takes to run. If it did, a slow computer would make every program look bad, and a fast computer would make every program look good. That is not useful.
Instead, time complexity measures how the number of steps grows as the input size grows. It is about the relationship between input and effort — not the actual clock time.
A story to make this stick forever
Imagine a teacher has a stack of exam papers to return. There are 30 students.
Teacher A's method grows with input size. Teacher B's method stays constant. That difference — that relationship between input and steps — is time complexity.
What is Space Complexity?
While time complexity is about speed, space complexity is about memory. It measures how much extra memory your program uses as the input size grows.
The moving house analogy again
In code, this means: does your program create extra variables, arrays, or data structures that grow with the input? Or does it work with a fixed small amount of memory no matter how large the input is?
Big O Notation — The Universal Language
Big O notation is how engineers all over the world describe complexity in a standard way. When someone says "this algorithm is O(n²)" — every engineer on earth knows exactly what that means.
The letter n always represents the size of the input. If you have an array of 100 elements, n = 100. If you have 1 million users, n = 1,000,000. Big O describes what happens to the number of steps as n grows.
Best, Average, and Worst Case
The same algorithm can behave very differently depending on what data you give it. This is why we talk about three cases.
Suppose you are searching for a person in a list of 1000 names, going one by one:
How to Analyse Any Code — Step by Step
Now let us actually apply all of this. Here is the method — four simple rules you can apply to any piece of code to figure out its Big O complexity.
Example 1 — O(1) Constant Time
No matter what value n has, this function always does exactly the same number of steps.
Example 2 — O(n) Linear Time
The loop runs once for every element. More elements = more steps, directly.
Example 3 — O(n²) Quadratic Time
A loop inside a loop. The inner loop runs n times for every iteration of the outer loop. Total steps = n × n.
Example 4 — Mixed: What is the complexity here?
When you have multiple parts, identify the dominant one.
O(1) + O(n) + O(n²)→O(n²)(n² dominates for large n)O(n) + O(n) + O(n)→O(n)(3n simplifies to n — drop constants)O(n²) + O(n log n)→O(n²)(n² grows faster than n log n)What These Numbers Actually Mean
Let us make this real. Here is how many operations each complexity class produces for different input sizes. Assume your computer does 1 billion operations per second.
| n (input size) | O(1) | O(log n) | O(n) | O(n²) | O(2ⁿ) |
|---|---|---|---|---|---|
| 10 | 1 | 3 | 10 | 100 | 1,024 |
| 100 | 1 | 7 | 100 | 10,000 | 1.3 × 10³⁰ |
| 1,000 | 1 | 10 | 1,000 | 1,000,000 | > universe |
| 1,000,000 | 1 | 20 | 1,000,000 | 10¹² | > universe |
You Are Ready for Lesson 3
You now have the single most important mental tool in all of DSA — the ability to look at any piece of code and judge whether it is efficient or not. Every lesson from here uses this language. When we say "arrays give O(1) access" or "bubble sort is O(n²)", you now know exactly what that means.
In Lesson 3 we dive into Arrays — the very first and most fundamental data structure. We will build them from scratch in C, understand exactly how they sit in memory, and write the core operations: insert, delete, search, and traverse.
🎯 Key Takeaways
- ✓Time complexity measures how the number of steps grows as input size (n) grows — not actual clock time
- ✓Space complexity measures how much extra memory your program uses as n grows
- ✓Big O notation is the standard language: O(1) < O(log n) < O(n) < O(n log n) < O(n²) < O(2ⁿ)
- ✓O(1) = constant — same steps no matter what. O(n) = linear — steps grow with n. O(n²) = nested loops
- ✓Always analyse for worst case — a system that fails under bad input is not reliable
- ✓When combining complexities, keep only the dominant term — O(n² + n) simplifies to O(n²)
- ✓Exponential O(2ⁿ) algorithms are never used in real systems — they become impossibly slow instantly
Quick check
What is the time complexity of a loop inside a loop, each running n times?
HELP US IMPROVE
How was this lesson?
Your note is shared with the Chaduvuko team for review. Please don’t include personal or sensitive information.
Spotted a mistake or something unclear? Suggest a fix on GitHub or email us. Select the text first and it is quoted for you.
Discussion
0Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.
