Common complexity classes (O(1), O(n), O(log n), O(n²)) in Data Structures Theory - Time & Space Complexity
Time complexity helps us understand how the time to run a task changes as the input grows.
We want to know how fast or slow common patterns like constant, linear, logarithmic, and quadratic growth are.
Look at these simple code examples showing different time complexities.
// O(1) - constant time
function getFirstItem(array) {
return array[0];
}
// O(n) - linear time
function printAllItems(array) {
for (let item of array) {
console.log(item);
}
}
// O(log n) - logarithmic time
function binarySearch(array, target) {
let left = 0, right = array.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (array[mid] === target) return mid;
else if (array[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}
// O(n²) - quadratic time
function printPairs(array) {
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length; j++) {
console.log(array[i], array[j]);
}
}
}
Each function shows a different way the number of steps grows with input size.
Look at what repeats in each example:
- O(1): Just one step, no loops.
- O(n): One loop over all items, runs n times.
- O(log n): Loop cuts the problem size roughly in half each time, runs about log n times.
- O(n²): Two nested loops, each runs n times, total about n x n = n² times.
Here is how the number of steps changes as input size grows:
| Input Size (n) | O(1) | O(n) | O(log n) | O(n²) |
|---|---|---|---|---|
| 10 | 1 | 10 | ~4 | 100 |
| 100 | 1 | 100 | ~7 | 10,000 |
| 1000 | 1 | 1000 | ~10 | 1,000,000 |
Notice how constant stays the same, linear grows steadily, logarithmic grows slowly, and quadratic grows very fast.
Time Complexity: O(1), O(n), O(log n), O(n²)
These describe how the steps needed grow as input size grows, from fastest (constant) to slowest (quadratic).
[X] Wrong: "Logarithmic time means the code runs instantly no matter the input size."
[OK] Correct: Logarithmic time still grows as input grows, just much slower than linear. It's fast but not instant.
Understanding these common time complexities helps you explain how efficient your code is and shows you can think about performance clearly.
"What if we replaced the inner loop in the quadratic example with a loop that runs only half as many times? How would the time complexity change?"
Practice
Solution
Step 1: Understand what O(1) means
O(1) means constant time, so the algorithm's time does not change with input size.Step 2: Compare with other options
O(n), O(log n), and O(n²) all depend on input size and grow as input grows.Final Answer:
O(1) -> Option DQuick Check:
Constant time = O(1) [OK]
- Confusing O(1) with O(n)
- Thinking O(log n) is constant
- Mixing up growth rates
Solution
Step 1: Understand linear growth
Doubling work when the input size doubles means time grows directly with input size, which is linear.Step 2: Match with complexity classes
O(n) means time grows linearly with input size, so it fits the description.Final Answer:
O(n) -> Option CQuick Check:
Linear growth = O(n) [OK]
- Choosing O(log n) for linear growth
- Confusing O(n²) with linear
- Thinking O(1) grows with input
for i in range(n):
for j in range(n):
print(i, j)What is the time complexity of this code?
Solution
Step 1: Analyze the outer loop
The outer loop runs n times, from 0 to n-1.Step 2: Analyze the inner loop
For each iteration of the outer loop, the inner loop runs n times.Step 3: Calculate total iterations
Total iterations = n (outer) * n (inner) = n².Final Answer:
O(n²) -> Option BQuick Check:
Nested loops each n times = O(n²) [OK]
- Counting only one loop
- Thinking nested loops are O(n)
- Confusing with O(log n)
def search(lst, target):
for i in range(len(lst)):
if lst[i] == target:
return True
return FalseWhat is the main issue with this code regarding time complexity?
Solution
Step 1: Identify current complexity
The code uses a simple loop over the list, so it is O(n).Step 2: Consider the list is sorted
For sorted lists, binary search can be used to reduce time to O(log n).Final Answer:
It uses O(n) but could be improved to O(log n) -> Option AQuick Check:
Linear search on sorted list = O(n), binary search = O(log n) [OK]
- Thinking linear search is O(log n)
- Confusing O(n²) with O(n)
- Believing O(1) is possible here
Solution
Step 1: Evaluate linear search
Linear search checks each element, O(n) = 1,000,000 steps on average.Step 2: Evaluate binary search
Binary search halves the search space each step, O(log n) ≈ 20 steps for 1,000,000 elements.Step 3: Evaluate nested loops and constant time
Nested loops O(n²) is very slow; O(1) without extra structures is unrealistic here.Final Answer:
Binary search on sorted list with O(log n) complexity -> Option AQuick Check:
Binary search fastest for large sorted lists = O(log n) [OK]
- Choosing linear search for large data
- Expecting O(1) without extra structures
- Ignoring sorting requirement
Ready to prove it?
Pass the test and this pattern moves unknown
