Time Complexity Of This Code Snippet
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Analyzing the time complexity of a code snippet means counting how the number of operations grows relative to the input size n. The standard tool for this is Big O notation, which describes the upper bound of growth rate, ignoring constant factors and lower-order terms. The key patterns are: single loops are O(n), nested loops are O(n²), halving loops are O(log n), and loops where the inner bound depends on the outer variable require careful summation.
Pattern 1: Single Loop — O(n)
The loop runs n times, each iteration does constant work.
Pattern 2: Nested Loops — O(n²)
Each iteration of the outer loop triggers n iterations of the inner loop.
Pattern 3: Nested Loop with Dependent Bound — O(n²)
The inner loop runs 0 + 1 + 2 + ... + (n-1) = n(n-1)/2 times total. This is O(n²).
Pattern 4: Halving Loop — O(log n)
Each iteration halves the value, so it takes log₂(n) steps to reach 0. This is the pattern in binary search.
Pattern 5: Doubling Loop — O(log n)
Pattern 6: Outer Linear, Inner Logarithmic — O(n log n)
This is the pattern seen in efficient sorting algorithms like merge sort and heap sort.
Pattern 7: Triple Nested Loop — O(n³)
Pattern 8: Loop with Multiplicative Step — O(log n)
Any multiplicative step (×2, ×3, ×10) produces logarithmic iterations. The base of the logarithm is absorbed into the constant factor in Big O.
Pattern 9: Two Separate Loops — O(n)
Sequential loops add their complexities. O(n) + O(n) = O(2n) = O(n).
Pattern 10: Inner Loop Runs Constant Times — O(n)
The inner loop runs a fixed number of times regardless of n. Constants are dropped in Big O.
Analyzing a Complex Snippet
Space Complexity Quick Reference
| Pattern | Space |
| Fixed number of variables | O(1) |
| Array of size n | O(n) |
| 2D matrix n×n | O(n²) |
| Recursive call stack depth d | O(d) |
Common Pitfalls
- Ignoring the inner loop's dependency on the outer variable:
for j in range(i)insidefor i in range(n)is not O(n) — it is O(n²) because the inner iterations sum to n(n-1)/2. Always compute the total by summing across all outer iterations. - Confusing O(log n) bases: O(log₂ n) and O(log₁₀ n) are the same complexity class because they differ by a constant factor (log₂ n = log₁₀ n / log₁₀ 2). The base does not matter in Big O.
- Assuming nested loops are always O(n²): If the inner loop runs a constant number of times (e.g.,
for j in range(10)), the total is O(n), not O(n²). Only loops whose bounds grow with n contribute to the complexity. - Forgetting amortized analysis: Operations like
list.append()in Python are O(1) amortized even though occasional resizing is O(n). A loop appending n elements is O(n) total, not O(n²). - Counting recursive calls incorrectly: A function making two recursive calls with n/2 input each creates 2^(log n) = n total calls (like merge sort), giving O(n log n) with O(n) work per level. Drawing the recursion tree helps visualize the total work.
Summary
- Single loop over n elements: O(n)
- Nested loops with independent bounds: O(n²), O(n³), etc.
- Loop halving or doubling: O(log n)
- Outer O(n) with inner O(log n): O(n log n)
- Dependent inner loops: Sum the iterations (e.g., 0+1+2+...+(n-1) = O(n²))
- Sequential (non-nested) loops add: O(n) + O(m) = O(n + m)
- Constants and lower-order terms are dropped in Big O
Related reading
- Time Complexity of two for loops
- Time complexity to generate all pairs in an array
- Time/Space Complexity of Depth First Search
- Tinyurl-style unique code potential algorithm to prevent collisions
- Time cost of training with pytorch DDP with multi-GPUs
- Timed annotation in spring metrics
- Tips implementing permutation algorithm in Java
- To make a distance matrix or to repeatedly calculate distance

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.