Time complexity (Big O notation) in Data Structures Theory - Time & Space Complexity
Time complexity helps us understand how the time to run a program grows as the input gets bigger.
We want to know how the work done changes when we add more data.
Analyze the time complexity of the following code snippet.
function sumArray(arr) {
let total = 0;
for (let i = 0; i < arr.length; i++) {
total += arr[i];
}
return total;
}
This code adds up all the numbers in an array and returns the total.
- Primary operation: The for-loop that visits each item in the array once.
- How many times: Exactly once for each element in the array.
As the array gets bigger, the number of steps grows in a straight line with the number of items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps |
| 100 | 100 steps |
| 1000 | 1000 steps |
Pattern observation: Doubling the input doubles the work needed.
Time Complexity: O(n)
This means the time to finish grows directly with the size of the input.
[X] Wrong: "The loop runs faster because it just adds numbers, so time doesn't grow much."
[OK] Correct: Even simple steps add up when repeated many times, so time still grows with input size.
Understanding how time grows with input size helps you explain your code's efficiency clearly and confidently.
"What if we added a nested loop inside the first loop? How would the time complexity change?"
Practice
O(n) time complexity mean in simple terms?Solution
Step 1: Understand the meaning of
O(n)O(n)means the time grows in direct proportion to the input sizen.Step 2: Compare with other growth types
Unlike constant timeO(1)or logarithmicO(log n),O(n)means if input doubles, time roughly doubles.Final Answer:
The running time grows linearly with input size -> Option BQuick Check:
Linear growth = O(n) [OK]
- Confusing O(n) with constant time O(1)
- Thinking O(n) means exponential growth
- Mixing up logarithmic and linear growth
Solution
Step 1: Recall constant time definition
Constant time means the operation takes the same time regardless of input size.Step 2: Match with Big O notation
O(1)represents constant time complexity.Final Answer:
O(1) -> Option AQuick Check:
Constant time = O(1) [OK]
- Choosing O(n) for constant time
- Confusing logarithmic with constant time
- Writing O(0) which is invalid
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 runsntimes.Step 2: Analyze the inner loop
For each iteration of the outer loop, the inner loop runsntimes.Step 3: Calculate total operations
Total operations = n * n = n², so time complexity isO(n^2).Final Answer:
O(n^2) -> Option DQuick Check:
Nested loops each running n times = O(n²) [OK]
- Counting only one loop's iterations
- Confusing O(n log n) with O(n²)
- Assuming constant time for nested loops
def example(n):
for i in range(n):
for j in range(10):
print(i, j)Claimed time complexity: O(n^2). What is the correct time complexity?
Solution
Step 1: Analyze the outer loop
The outer loop runsntimes.Step 2: Analyze the inner loop
The inner loop runs a fixed 10 times, which is constant.Step 3: Calculate total operations
Total operations = n * 10 = 10n, which simplifies toO(n).Final Answer:
O(n) -> Option AQuick Check:
Fixed inner loop means linear time = O(n) [OK]
- Treating fixed inner loop as variable
- Writing O(n^2) without checking inner loop range
- Confusing constant factors with growth rates
n items. It first sorts the list using an efficient algorithm, then scans it once to find duplicates.What is the overall time complexity of this function?
Solution
Step 1: Analyze sorting step
Efficient sorting algorithms like mergesort or heapsort run inO(n log n)time.Step 2: Analyze scanning step
Scanning the sorted list once isO(n).Step 3: Combine complexities
Total time is dominated by the slower step, which is sorting atO(n log n).Final Answer:
O(n log n) -> Option CQuick Check:
Sorting dominates scanning, so O(n log n) [OK]
- Adding complexities instead of taking max
- Assuming scanning adds to sorting complexity
- Confusing sorting time with linear time
Ready to prove it?
Pass the test and this pattern moves unknown
