Two sequential loops are O(n) — nesting multiplies complexity, addition does not.
Hidden complexity in loop bodies: any method call that itself loops adds another factor.
✦ Definition~90s read
What is Time Complexity Analysis?
Time complexity analysis is the mathematical framework for predicting how an algorithm's runtime scales as input size grows. It strips away hardware-specific factors like CPU speed or memory bandwidth to reveal the fundamental growth pattern — what happens when you double the input.
★
Imagine you're looking for a specific book in a library.
This is why Big O notation deliberately ignores constants and lower-order terms: a 2x faster machine doesn't change the fact that a quadratic algorithm on 10 million products requires 100 trillion operations, while a linear one needs only 10 million. The core insight is that for large inputs, the growth rate dominates everything else, making complexity analysis the single most important tool for predicting production performance before you write a line of code.
In practice, time complexity answers the question 'Will this work at scale?' A nested loop processing 10 million products (O(n²)) isn't just slow — it's catastrophically slow, requiring roughly 50 trillion comparisons if each product is compared to every other. Contrast this with O(n log n) sorting or O(1) hash lookups, which handle the same data in seconds.
The distinction between best, worst, and average cases matters because real-world inputs aren't uniform: a quicksort that averages O(n log n) degrades to O(n²) on already-sorted data, while a merge sort guarantees O(n log n) regardless. This is why production systems like Google's Bigtable or Amazon's DynamoDB explicitly document their worst-case guarantees — they're designing for the pathological case, not the happy path.
The ecosystem of complexity classes forms a hierarchy you internalize after enough production fires: O(1) (hash table lookups), O(log n) (binary search on 10M items takes ~24 steps), O(n) (single pass through data), O(n log n) (efficient sorting), O(n²) (nested loops — the killer of 10M products), and O(2^n) (exponential — unusable past n=30). When you're reading code, you become a complexity detective: a single loop is O(n), nested loops multiply to O(n²), and logarithms appear wherever you halve the problem space (binary search, balanced trees, divide-and-conquer).
Recognizing these patterns across languages — from C++'s std::sort (O(n log n)) to Python's list.sort (also O(n log n) Timsort) — lets you spot scaling problems before they hit production. The alternative is discovering at 3 AM that your 'simple' nested loop just killed 10 million products.
Plain-English First
Imagine you're looking for a specific book in a library. If the books are sorted alphabetically, you can jump straight to the right shelf — that's fast. If they're dumped in a random pile, you might check every single book — that's slow. Time complexity is just a way of measuring how many 'book checks' your code has to do as the library gets bigger. It doesn't measure seconds on a clock — it measures how the amount of work grows when you give your program more data.
Every developer has written code that worked perfectly on 100 rows of test data, then crawled to a halt on a million rows in production. That gap between 'it works' and 'it scales' is almost always a time complexity problem. Time complexity analysis is the tool that lets you predict performance before you hit production — and it's the difference between writing code that survives growth and code that embarrasses you in front of the CTO.
The core problem it solves is this: your machine is fast, so bad algorithms hide. A nested loop over 50 items feels instant. Over 500,000 items, that same loop can take minutes. Time complexity gives you a mathematical model — independent of hardware, language, and compiler tricks — to reason about how your algorithm behaves at scale. It's a shared language between engineers so you can say 'this is O(n²)' and immediately everyone knows it won't scale.
By the end of this article you'll be able to read any function and identify its time complexity, understand why Big O drops constants and lower-order terms, correctly distinguish between best, worst and average cases, and spot the complexity class of the most common algorithm patterns you'll encounter every day. You'll also walk away with the exact answers interviewers are looking for when they ask about this topic.
What Big O Actually Measures (And What It Deliberately Ignores)
Big O notation describes the rate of growth of an algorithm's runtime relative to its input size, typically called n. It intentionally throws away two things: constant factors and lower-order terms. That might feel like cheating at first, but there's a solid reason for it.
Constant factors are hardware-dependent. Multiplying all operations by 3 because your machine is 3x slower than the benchmarking machine tells you nothing useful about the algorithm itself. Big O strips that noise away so you're analyzing the algorithm, not the environment.
Lower-order terms matter less and less as n grows. If an algorithm does n² + 500n + 1000 operations, at n=10,000 the n² term is 100,000,000, while 500n is only 5,000,000 — already 20x smaller. At n=1,000,000, the gap is astronomical. Big O keeps only the term that will eventually dominate, because that's what determines real-world scalability.
The common complexity classes you'll encounter, from fastest to slowest growth
ComplexityGrowthDemo.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package io.thecodeforge.complexity;
publicclassComplexityGrowthDemo {
publicstaticvoidmain(String[] args) {
int[] inputSizes = {10, 100, 1_000, 10_000};
System.out.printf("%-12s %-12s %-12s %-15s%n",
"n", "O(n)", "O(n log n)", "O(n²)");
System.out.println("-".repeat(55));
for (int n : inputSizes) {
long linearOps = n; // grows in a straight line
long nLogNOps = (long)(n * (Math.log(n) / Math.log(2))); // grows slightly faster
long quadraticOps = (long) n * n; // explodes as n growsSystem.out.printf("%-12d %-12d %-12d %-15d%n",
n, linearOps, nLogNOps, quadraticOps);
}
// The takeaway: at n=10,000, O(n²) is already 10,000x MORE work than O(n).// That gap keeps growing. This is why complexity class matters.
}
}
O(2n) and O(n) are both written as O(n) because doubling a linear algorithm is still linear — it never changes the fundamental growth shape. If you have two algorithms, one O(2n) and one O(n²), the O(2n) one will always win for large enough n, no matter what the constants say.
Production Insight
The biggest mistake in production: optimizing constants instead of changing complexity class.
A 20% faster loop is dwarfed by moving from O(n²) to O(n log n).
Always invest in algorithm improvement before micro-optimizations.
Key Takeaway
Big O removes hardware and constants so you compare algorithms, not machines.
The dominant term decides scalability — $n^2$ always loses to $n \log n$ for large $n$.
Remember: you can't constant-factor your way out of a bad complexity class.
thecodeforge.io
Time Complexity Analysis — Nested Loop Killed 10M Products
Time Complexity Analysis
Best, Worst, and Average Case — Why the Distinction Actually Matters
This is where a lot of developers get sloppy. When someone says 'linear search is O(n)', they're talking about the worst case — the target element is at the very end, or not there at all. But the best case is O(1): the target is the first element you check. Average case sits somewhere in between.
The distinction matters in practice because different scenarios call for different analyses. If you're building a security-critical system, you care about worst case — an attacker will always find your worst input. If you're optimizing a recommendation engine where you have statistical knowledge of your input distribution, average case is more informative.
Java's own standard library makes this trade-off explicitly. ArrayList.get() is O(1) worst case because arrays support direct index access. LinkedList.get() is O(n) worst case because it has to walk the chain of nodes. On the surface they both 'get an element' — but their complexity profiles are radically different.
The most famous example of this distinction is Quicksort: average case O(n log n) but worst case O(n²) when the pivot selection is unlucky (e.g., always picking the smallest or largest element). That's why production implementations use median-of-three pivot selection or introsort — they're protecting against the worst case, not just optimizing the average.
LinearSearchCaseAnalysis.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package io.thecodeforge.complexity;
import java.util.Arrays;
publicclassLinearSearchCaseAnalysis {
/**
* Standard linear search — returns index of target, or -1if not found.
* Bestcase: O(1) — target is at index 0
* Worstcase: O(n) — target is at last index or absent
* Averagecase: O(n/2) => simplified to O(n) — target is somewhere in the middle
*/
public static int linearSearch(int[] numbers, int target) {\n int comparisonsCount = 0;\n\n for (int index = 0; index < numbers.length; index++) {\n comparisonsCount++; // count every comparison we make\n if (numbers[index] == target) {\n System.out.printf(\"Found %d at index %d after %d comparison(s)%n\",\n target, index, comparisonsCount);\n return index;\n }\n }\n\n System.out.printf(\"%d not found after %d comparison(s)%n\",\n target, comparisonsCount);\n return -1;\n }\n\n public static void main(String[] args) {\n int[] temperatures = {72, 65, 80, 91, 55, 88, 76, 60, 95, 70};\n\n System.out.println(\"Array: \" + Arrays.toString(temperatures));\n System.out.println(\"Array size: \" + temperatures.length + \" elements\");\n System.out.println();\n\n // Best case — target is the very first element: O(1)\n System.out.println(\"--- Best Case ---\");\n linearSearch(temperatures, 72);\n\n // Average case — target is somewhere in the middle: O(n/2) → O(n)\n System.out.println(\"\\n--- Average Case ---\");\n linearSearch(temperatures, 88);\n\n // Worst case — target is last OR not present at all: O(n)\n System.out.println(\"\\n--- Worst Case (last element) ---\");\n linearSearch(temperatures, 70);\n\n System.out.println(\"\\n--- Worst Case (not present) ---\");\n linearSearch(temperatures, 99);\n }\n}","output": "Array: [72, 65, 80, 91, 55, 88, 76, 60, 95, 70]\nArray size: 10 elements\n\n--- Best Case ---\nFound 72 at index 0 after 1 comparison(s)\n\n--- Average Case ---\nFound 88 at index 5 after 6 comparison(s)\n\n--- Worst Case (last element) ---\nFound 70 at index 9 after 10 comparison(s)\n\n--- Worst Case (not present) ---\n99 not found after 10 comparison(s)"
}
Big O Cheat Sheet: Common Algorithms by Complexity
Here is a quick reference table for the time complexity of common algorithms. This cheat sheet is useful during code reviews and interview preparation.
Algorithm
Best
Average
Worst
Linear Search
O(1)
O(n)
O(n)
Binary Search (sorted array)
O(1)
O(log n)
O(log n)
Bubble Sort
O(n)
O(n²)
O(n²)
Insertion Sort
O(n)
O(n²)
O(n²)
Selection Sort
O(n²)
O(n²)
O(n²)
Merge Sort
O(n log n)
O(n log n)
O(n log n)
Quick Sort
O(n log n)
O(n log n)
O(n²)
Heap Sort
O(n log n)
O(n log n)
O(n log n)
Binary Tree Search (balanced)
O(1)
O(log n)
O(log n)
BFS / DFS on graph (V vertices, E edges)
O(V + E)
O(V + E)
O(V + E)
Dijkstra’s Algorithm (with heap)
O(V log V + E)
O(V log V + E)
O(V log V + E)
Hash Table (ideal)
O(1)
O(1)
O(n)
Remember that the worst case for hash table is O(n) only if many collisions occur. In practice with a good hash function and resizing, it is effectively O(1) amortized.
Memorize These:
The most common complexities you'll encounter daily are O(1), O(n), O(n log n), and O(n²). Focus on knowing which algorithms fall into each category – it makes code review much faster.
Production Insight
When designing a system, choose an algorithm whose worst-case complexity matches your load. For a sorting routine that handles millions of records daily, O(n log n) is mandatory. O(n²) is acceptable only when n is guaranteed to stay small (e.g., < 1000).
Key Takeaway
This cheat sheet lets you instantly recognize the complexity of standard algorithms without re-deriving them – a must-have for production debugging and interviews.
Growth Rate at a Glance: Visualizing How Complexity Classes Compare
The following table shows the number of operations for each major complexity class at different input sizes. The visual below makes it clear why O(n²) becomes unusable for even moderate n.
n
O(1)
O(log n)
O(n)
O(n log n)
O(n²)
10
1
4
10
33
100
100
1
7
100
664
10,000
1,000
1
10
1,000
9,965
1,000,000
10,000
1
14
10,000
132,877
100,000,000
100,000
1
17
100,000
1,660,964
10,000,000,000
The growth gap is enormous: at n = 100,000, O(n²) performs about 10 billion operations while O(n log n) performs only 1.6 million. This visual should convince you that algorithm choice is the single most important performance decision.
Production Insight
Never assume that a small dataset today means a small dataset tomorrow. Always design for the worst-case growth. A microservice that handles 10k requests per second today will likely face 100k next year – choose O(n log n) over O(n²) from the start.
Key Takeaway
Visualizing the growth rate reinforces why complexity analysis is essential – one line of code can be the difference between 1 million and 10 billion operations.
Reading Code Like a Complexity Detective: Loops, Nesting & Logarithms
Once you internalize a few rules, you can read most functions and determine their complexity in under 30 seconds. Here's the mental model:
Single loops over n elements → O(n). The loop body runs once per element. If the body itself is O(1), the whole thing is O(n).
Nested loops over n elements → O(n²). The inner loop runs n times for each of the n outer iterations. This is the most common source of hidden performance problems — and the reason why writing two separate loops (O(n) + O(n) = O(n)) is dramatically better than nesting them (O(n²)).
Halving the problem each step → O(log n). Binary search, balanced BST lookups, and anything that repeatedly cuts the remaining work in half. The key insight: to process a billion elements with O(log n), you only need about 30 steps. That's the power of logarithmic growth.
A loop that does O(log n) work per iteration → O(n log n). This is the signature of efficient sorting algorithms like merge sort and heapsort. It's the sweet spot: fast enough for almost any real dataset.
The trick with complex functions is to break them into their individual parts, find the complexity of each part, and keep only the dominant term when they're sequential, or multiply them when they're nested.
ComplexityPatternExamples.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package io.thecodeforge.complexity;
publicclassComplexityPatternExamples {
// O(1) — constant time, no matter how big the list ispublicstaticStringgetFirstCity(String[] cities) {
return cities[0]; // direct index access, always one operation
}
// O(n) — single loop, one pass through all elementspublicstaticdoublecalculateAverageTemperature(double[] readings) {
double total = 0;
for (double temp : readings) { // visits each element exactly once
total += temp;
}
return total / readings.length;
}
// O(n²) — nested loops, classic quadratic pattern// This checks every pair of products for duplicate names.publicstaticbooleanhasDuplicateProductName(String[] productNames) {
for (int outerIndex = 0; outerIndex < productNames.length; outerIndex++) {
for (int innerIndex = outerIndex + 1; innerIndex < productNames.length; innerIndex++) {
// inner loop runs (n-1) + (n-2) + ... + 1 times total = n(n-1)/2 → O(n²)if (productNames[outerIndex].equals(productNames[innerIndex])) {
returntrue;
}
}
}
returnfalse;
}
// O(log n) — binary search, problem halves every iterationpublicstaticintfindPriceIndex(int[] sortedPrices, int targetPrice) {
int left = 0;
int right = sortedPrices.length - 1;
int stepCount = 0;
while (left <= right) {
stepCount++;
int midpoint = left + (right - left) / 2; // avoids integer overflow vs (left+right)/2if (sortedPrices[midpoint] == targetPrice) {
System.out.println("Binary search found in " + stepCount + " step(s)");
return midpoint;
} elseif (sortedPrices[midpoint] < targetPrice) {
left = midpoint + 1; // discard left half
} else {
right = midpoint - 1; // discard right half
}
}
return -1;
}
publicstaticvoidmain(String[] args) {
// O(1) demoString[] capitals = {"Paris", "Tokyo", "Cairo", "Sydney"};
System.out.println("First city: " + getFirstCity(capitals));
// O(n) demodouble[] weeklyTemps = {68.5, 72.1, 65.0, 80.3, 77.8, 69.2, 74.6};
System.out.printf("Average temp: %.2f°F%n", calculateAverageTemperature(weeklyTemps));
// O(n²) demoString[] inventory = {"Widget", "Gadget", "Doohickey", "Widget"};
System.out.println("Has duplicate product name: " + hasDuplicateProductName(inventory));
// O(log n) demo — array MUST be sorted for binary searchint[] sortedPrices = {5, 12, 23, 37, 45, 58, 67, 82, 91, 99,
104, 118, 135, 147, 160}; // 15 elementsint targetIndex = findPriceIndex(sortedPrices, 147);
System.out.println("Price $147 found at index: " + targetIndex);
// With 15 elements, log₂(15) ≈ 4 steps worst case — versus 15 for linear search
}
}
Output
First city: Paris
Average temp: 72.50°F
Has duplicate product name: true
Binary search found in 4 step(s)
Price $147 found at index: 13
Pro Tip: Spotting O(log n) in the Wild
Any time you see a variable that's divided by 2 each loop iteration (or multiplied — like doubling a range), that's your logarithmic signal. while (n > 1) { n = n / 2; } runs log₂(n) times. This pattern appears in binary search, power-of-two checks, segment trees, and heap operations.
Production Insight
Hidden O(log n) isn't a problem — but hidden O(n) inside a loop that you thought was constant is a disaster.
A common trap: calling list.contains() (O(n)) inside a loop that itself is O(n). That's O(n²), not O(n).
Always audit method calls in loops: if the method's complexity isn't O(1), it multiplies.
Key Takeaway
Recognize the patterns: single loop = O(n), nested = O(n²), halving = O(log n).
The most common production mistake: treating O(n) method calls inside a loop as constant.
C++ Code Examples: Recognizing Complexity Classes Across Languages
The same complexity patterns appear in C++ with slightly different syntax and standard library calls. Here are the equivalent examples in C++ using STL containers and algorithms.
O(1) – Constant: Accessing any element of a std::vector via operator[] is O(1). Similarly
In C++ production, always be aware of hidden complexity: std::list::size() was O(n) before C++11, and std::vector::push_back is amortized O(1) but can trigger reallocation. Use std::back_inserter with pre-allocated space to avoid repeated resizes.
Key Takeaway
Complexity is language-independent. Learn the patterns once, apply them everywhere. The STL containers and algorithms mirror the same Big O classes as Java collections.
Analyzing Real-World Code: Two Loops Aren't Always O(n²)
One of the most persistent misconceptions is that 'two loops = O(n²)'. That's only true when the loops are nested. Two loops running one after the other (sequentially) are O(n) + O(n) = O(2n), which simplifies to O(n). This matters a lot when refactoring.
There's also the case where two loops iterate over differentvariables. A loop over n products and a nested loop over m reviews per product is O(n × m), not O(n²). If m is small and bounded (say, max 10 reviews), this is effectively O(n) — the inner loop is a constant multiplier. But if m can also scale with input, you need to track both dimensions.
Amortized complexity is another real-world nuance. Java's ArrayList.add() is O(1) amortized — most adds are truly O(1), but occasionally the backing array needs to be resized, which is O(n). Averaged across many operations, it works out to O(1) per add. If you called this 'O(n)' in an interview without the amortized qualifier, a sharp interviewer would push back.
Understanding these subtleties is what separates someone who memorized Big O from someone who can actually apply it to real code reviews — which is exactly the kind of engineer companies want to hire.
SequentialVsNestedLoops.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package io.thecodeforge.complexity;
import java.util.ArrayList;
import java.util.List;
publicclassSequentialVsNestedLoops {
// O(n) — two SEQUENTIAL loops, not nested// Total work = n + n = 2n → simplifies to O(n)publicstaticvoidprintAllOrderSummaries(String[] orderIds, double[] orderTotals) {
// First pass: print all order IDs — O(n)System.out.println("=== Order IDs ===");
for (String orderId : orderIds) {
System.out.println(" Order: " + orderId);
}
// Second pass: print all totals — O(n)// These are NOT nested, so the total is O(n) + O(n) = O(n)System.out.println("=== Order Totals ===");
for (double total : orderTotals) {
System.out.printf(" $%.2f%n", total);
}
}
// O(n × m) — nested loops over DIFFERENT size variables// If m is bounded (e.g. always ≤ 5 tags), this is effectively O(n)// If m scales with input size, treat it as O(n × m)publicstaticvoidprintProductsWithTags(List<String> productNames,
List<List<String>> productTags) {\n System.out.println(\"=== Products and Tags ===\");\n for (int productIndex = 0; productIndex < productNames.size(); productIndex++) {\n // outer loop: n products\n System.out.print(\" \" + productNames.get(productIndex) + \" → tags: \");\n\n List<String> tags = productTags.get(productIndex);\n for (String tag : tags) { // inner loop: m tags per product\n System.out.print(\"[\" + tag + \"] \");\n }\n System.out.println();\n }\n }\n\n public static void main(String[] args) {\n // Demo sequential loops — O(n), NOT O(n²)\n String[] orderIds = {\"ORD-001\", \"ORD-002\", \"ORD-003\"};\n double[] orderTotals = {49.99, 129.50, 18.75};\n printAllOrderSummaries(orderIds, orderTotals);\n\n System.out.println();\n\n // Demo nested loops over different variables — O(n × m)\n List<String> products = List.of(\"Laptop\", \"Headphones\", \"Keyboard\");\n List<List<String>> tags = new ArrayList<>();\n tags.add(List.of(\"electronics\", \"computing\")); // 2 tags\n tags.add(List.of(\"audio\", \"wireless\", \"accessories\")); // 3 tags\n tags.add(List.of(\"peripherals\", \"computing\")); // 2 tags\n // Here m (tags per product) is bounded and small — effectively O(n)\n printProductsWithTags(products, tags);\n }\n}","output": "=== Order IDs ===\n Order: ORD-001\n Order: ORD-002\n Order: ORD-003\n=== Order Totals ===\n $49.99\n $129.50\n $18.75\n\n=== Products and Tags ===\n Laptop → tags: [electronics] [computing] \n Headphones → tags: [audio] [wireless] [accessories] \n Keyboard → tags: [peripherals] [computing] "
}
Amortized Complexity: Why ArrayList.add Is O(1) (Almost)
Amortized analysis is a technique for averaging the cost of a sequence of operations. It gives a realistic view of performance when occasional expensive operations are balanced by many cheap ones.
Consider Java's ArrayList. When you call add(), most of the time it simply places the element in the next available slot of the underlying array — that's O(1). But when the array is full, a new array (typically 1.5× larger for Java, 2× for some implementations) is allocated and all existing elements are copied over — that's O(n). If you insert n elements, the total cost is O(n) for the cheap adds plus O(n) for the resizes (because each element is copied at most a constant number of times), so the amortized cost per add is O(1).
This same principle applies to other data structures: std::vector::push_back (C++), HashMap resizing, and dynamic arrays in Python and JavaScript. Understanding amortized complexity is crucial for choosing the right data structure. For example, if you frequently insert at positions other than the end, ArrayList's O(n) shift makes it a poor choice, even though appends are amortized O(1).
AmortizedArrayListDemo.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package io.thecodeforge.complexity;
import java.util.ArrayList;
publicclassAmortizedArrayListDemo {
publicstaticvoidmain(String[] args) {
// Demonstrate that adding many elements is O(n) total, not O(n²)int n = 100_000;
ArrayList<Integer> list = newArrayList<>();
long startTime = System.nanoTime();
for (int i = 0; i < n; i++) {
list.add(i);
}
long endTime = System.nanoTime();
long elapsedMs = (endTime - startTime) / 1_000_000;
System.out.println("Added " + n + " elements in " + elapsedMs + " ms");
System.out.println("That's amortized O(1) per add — total O(n)");
// To see the occasional resize, we can check capacity internally (though not public)// The point: copying happens only log(n) times, each doubling the array.
}
}
Output
Added 100000 elements in 12 ms
That's amortized O(1) per add — total O(n)
Amortized ≠ Guaranteed O(1)
Amortized O(1) does not mean every individual operation is O(1). In a real-time system where consistent latency is critical, the occasional O(n) resize could cause a timeout. In such cases, use a data structure with guaranteed O(1) insertions (e.g., LinkedList) or pre-allocate capacity.
Production Insight
When processing millions of additions, ArrayList is generally faster than LinkedList due to cache locality. The amortized O(1) append makes it the default choice. However, if you need to insert near the front, LinkedList (or ArrayDeque for double-ended operations) is better.
Key Takeaway
Amortized analysis shows that the cost of occasional resizing is averaged over all operations, giving realistic performance expectations for dynamic arrays.
Interview Strategies: How to Talk About Time Complexity Like a Senior Engineer
In coding interviews, time complexity analysis is table stakes. The interviewer expects you to state the complexity of your solution, explain why, and compare it to alternatives. But senior engineers do more than that — they talk about trade-offs.
Here's the difference: a junior says 'This is O(n²) and I can't fix it.' A senior says 'This is O(n²) because of the nested loops over reviews and products. I could reduce it to O(n log n) by sorting first and using binary search, but that would increase space complexity to O(n). Given our throughput requirements, O(n²) might be acceptable because the product count is bounded by 1000.'
Practice analyzing code on the fly. Use the rules from the previous sections. Always check if there are hidden O(n) calls inside your loops — that's the most common interview trap.
The second trap is forgetting to mention the case. If you say 'binary search is O(log n)', the interviewer will instantly ask 'which case?'. Always qualify: worst-case O(log n), best-case O(1). The same goes for any algorithm.
Finally, be ready to discuss space complexity alongside time complexity. A solution that is O(n) time but O(n) space might be acceptable; one that is O(n) time but O(2ⁿ) space is not. Senior engineers always consider both axes.
InterviewComplexityAnalyzer.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package io.thecodeforge.complexity;
import java.util.*;
publicclassInterviewComplexityAnalyzer {
// Example interview problem: find if any two numbers in an array sum to target// Brute force: O(n²) time, O(1) spacepublicstaticbooleanhasPairSum_BruteForce(int[] numbers, int target) {\n for (int i = 0; i < numbers.length; i++) {\n for (int j = i + 1; j < numbers.length; j++) {\n if (numbers[i] + numbers[j] == target) {\n returntrue;\n }
}
}
returnfalse;
}
// Optimized: O(n) time, O(n) space using HashSetpublicstaticbooleanhasPairSum_Optimized(int[] numbers, int target) {\n Set<Integer> seen = newHashSet<>();\n for (int num : numbers) {\n int complement = target - num;\n if (seen.contains(complement)) {\n returntrue;\n }
seen.add(num);
}
returnfalse;
}
// For sorted array: O(n log n) due to binary search per element? No
Output
Brute force (O(n²)): true
HashSet (O(n)): true
Two-pointer after sort (O(n log n)): true
Interview Complexity Analysis: The 3-Step Mental Model
Treat every code snippet as a puzzle: identify the loops, method calls, and recursion — then combine.
1. Identify all loops and recursive calls. Count how many times each runs relative to input size.
2. Check method calls inside loops. If a method is not O(1), multiply its complexity with the loop.
3. Remove constants and lower-order terms. State the result with the case (worst/average/best).
Production Insight
In interviews, the worst mistake is giving the wrong case without clarification. Always say 'worst case O(n)' not just 'O(n)'.
Another trap: assuming a built-in method is O(1) when it's not. For example, String.contains() is O(n) — call it in a loop and you've got O(n²).
Senior engineers explicitly call out these assumptions: 'This method is O(1) on average, but worst-case O(n) for hash collisions.'
Key Takeaway
Always state which case you're analyzing — worst, average, or best.
Beware hidden complexity in library calls — they are not all O(1).
Discuss trade-offs: time vs. space, worst-case vs. typical, and constant factors.
Practice: Identify the Complexity of These Code Snippets
Test your skills! For each snippet below, identify the time complexity (worst-case unless specified). Answers follow.
Snippet 1 ``java for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { System.out.println(i j); } } `` Hint: The inner loop runs 0, 1, 2, ... up to n-1 times.*
Snippet 2 ``java int result = 0; for (int i = 1; i < n; i *= 2) { result += i; } ``
Snippet 3 ``java int count = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { count++; } } } ``
Snippet 4 ``java int findMax(int[] arr) { int max = arr[0]; for (int num : arr) { if (num > max) max = num; } return max; } ``
Answers: 1. O(n²) – The inner loop runs i times for each i, total sum 0+1+...+n-1 = n(n-1)/2 ≈ n²/2. 2. O(log n) – i doubles each iteration, so number of iterations is log₂(n). 3. O(n³) – Three nested loops each over n, total n³ operations. 4. O(n) – Single pass over the array. 5. O(2ⁿ) – Each call spawns two recursive calls, leading to exponential growth.
Further Practice: - [LeetCode Complexity Analysis Tag](https://leetcode.com/tag/complexity/) - [GeeksforGeeks Practice Problems](https://www.geeksforgeeks.org/practice-for-cracking-any-coding-interview/) - [HackerRank Algorithm Section](https://www.hackerrank.com/domains/algorithms) - [InterviewBit Time Complexity Problems](https://www.interviewbit.com/courses/programming/time-complexity/)
Production Insight
Practicing complexity identification on code snippets prepares you for real code reviews. When you can spot O(n²) in seconds, you catch performance bugs before they reach production. Regularly review open source code to strengthen this skill.
Key Takeaway
The best way to internalize complexity analysis is to practice on many examples. Use the resources above to sharpen your ability to spot complexity classes in any code.
Logarithmic Time: The One That Trips Everyone Up
O(log n) is where the magic happens. You don't touch every element; you cut the problem in half at each step. That's why binary search on a billion elements takes only 30 iterations, while linear search would take a billion.
The pattern is always the same: your loop variable gets multiplied or divided by a constant. for (int i = 1; i < n; i *= 2) or while (high - low > 0). That's it. No nested loops. No magic. The base of the logarithm doesn't matter for Big O — log₂, log₁₀, ln, they're all the same growth rate because they differ by a constant factor.
Where people screw up is mistaking O(log n) for O(n). If you're halving the input and then iterating over half (like in merge sort's merge step), that's not O(log n). That's O(n log n). Don't be that person in code review.
BinarySearch.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// io.thecodeforge — dsa tutorialpublicclassBinarySearch {
publicstaticintfindCustomer(int[] customerIds, int target) {
int low = 0;
int high = customerIds.length - 1;
int iterations = 0;
while (low <= high) {
iterations++;
int mid = low + (high - low) / 2;
if (customerIds[mid] == target) {
System.out.println("Found in " + iterations + " iterations");
return mid;
} elseif (customerIds[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
System.out.println("Not found after " + iterations + " iterations");
return -1;
}
publicstaticvoidmain(String[] args) {
int[] ids = {1, 3, 5, 7, 9, 11, 13, 15};
findCustomer(ids, 13);
}
}
Output
Found in 3 iterations
Production Trap:
Bottlenecks hide in plain sight. That 'simple' while loop that shaves down n by subtraction, not division, is O(n), not O(log n). Check your loop variable update — if it's not multiplicative, you're probably linear.
Key Takeaway
If you're repeatedly dividing the problem space, it's O(log n). If you're stepping through one by one, it's O(n). That's the whole distinction.
How to Combine Time Complexities of Consecutive Loops (Like an Adult)
You have two loops back to back. First one is O(n), second is O(m). What's the total? It's O(n + m), unless one dominates. If they're both O(n), you've got O(2n), which simplifies to O(n). Stop overthinking.
The rule is dead simple: when loops are sequential, you add the complexities; when they're nested, you multiply. Adding: O(n) + O(m) = O(max(n, m)). That's because Big O only cares about the dominant term as input grows. If n is a million and m is ten, the O(n) loop drowns out the O(m) loop.
Where it gets spicy is when loops share the same input. If you iterate over the same array twice, it's O(2n) = O(n). If you iterate over it once and then sort it (O(n log n)), the total is O(n log n) because sorting dominates. Know your dominant term. Drop the constants. Move on.
SequentialLoops.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// io.thecodeforge — dsa tutorialpublicclassSequentialLoops {
publicstaticvoidprocessOrders(int[] orders) {
// O(n) — validate each orderfor (int order : orders) {
if (order <= 0) {
thrownewIllegalArgumentException("Invalid order: " + order);
}
}
// O(n) — apply discount to each orderfor (int i = 0; i < orders.length; i++) {
orders[i] = (int)(orders[i] * 0.9);
}
// O(n log n) — sort orders by value
java.util.Arrays.sort(orders);
}
publicstaticvoidmain(String[] args) {
int[] customerOrders = {100, 250, 50, 400};
processOrders(customerOrders);
System.out.println("Processed orders: " + java.util.Arrays.toString(customerOrders));
}
}
Output
Processed orders: [45, 90, 225, 360]
Senior Shortcut:
When explaining complexity in code review, just state the heaviest loop. Nobody cares that you added three O(n) loops — that's still O(n). Focus on the part that actually dominates.
Key Takeaway
Add for sequential, multiply for nested. Then drop the constants and keep only the biggest term. It's that boring.
If-Else Inside Loops: Why You're Probably Already Right (and Wrong)
Here's the trap: people think if-else inside a loop changes time complexity. It doesn't, unless the branches do different amounts of work. An if-else is O(1) — a single comparison and a jump. Your loop structure is what matters.
The only time an if-else breaks complexity is when one branch contains a loop, and the other doesn't. Then you analyze the worst-case path. If the if branch runs a nested loop (O(n)) and the else branch runs in constant time, the overall is O(n) per iteration. But if the if branch runs only once (e.g., on the first element), you still have to account for it — Big O is worst-case.
What kills performance is not the condition itself but how many times you execute the heavy branch. If your condition splits the input 50/50, and one side is O(n²), your average is still O(n²). You can't clever your way out of a nested loop with an if statement. Trust me, I've seen it tried.
An if-else with O(1) branches is fine. But if you see a loop with a condition triggering an inner loop, that's O(n*m) in disguise. Refactor or accept the cost.
Key Takeaway
If-else doesn't add complexity unless its branches have different Big O. When they do, use worst-case. When they don't, stop worrying about the condition.
Little o: The Asymptotic Ceiling That Never Catches Up
Big O said 'f grows no faster than g'. Little o says 'f grows strictly slower than g — and it never catches up, not even in the limit'. This is the difference between 'you'll never exceed this bound' and 'you'll always trail behind'.
In production, little o matters when you're proving an algorithm is strictly better than another asymptotically. Merge sort is O(n log n). It is not o(n log n) because constant factors aside, it actually is n log n. But linear search? That's o(n log n) for any input — it's strictly slower growing.
Use little o for dominance proofs. If an algorithm is o(n²), it's also O(n²), but the reverse is false. The hole in the floor is bigger than the hole in the ceiling. Small distinction, massive difference in interview depth.
LittleOExample.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// io.thecodeforge — dsa tutorial// Little o: f = x^1.5 grows strictly slower than g = x^2publicclassLittleOExample {
staticdoublef(int x) { returnMath.pow(x, 1.5); }
staticdoubleg(int x) { returnMath.pow(x, 2.0); }
publicstaticvoidmain(String[] args) {
// For any positive constant c, eventually f(x) < c * g(x)double c = 100.0;
for (int x = 1; x <= 100_000; x *= 10) {
boolean isLittleO = f(x) < c * g(x);
System.out.printf("x=%d: f(x)/g(x)=%.6f, f < %.0f*g? %b%n",
x, f(x)/g(x), c, isLittleO);
}
}
}
Output
x=1: f(x)/g(x)=1.000000, f < 100*g? true
x=10: f(x)/g(x)=0.316228, f < 100*g? true
x=100: f(x)/g(x)=0.100000, f < 100*g? true
x=1000: f(x)/g(x)=0.031623, f < 100*g? true
x=10000: f(x)/g(x)=0.010000, f < 100*g? true
x=100000: f(x)/g(x)=0.003162, f < 100*g? true
Common Mistake:
Don't conflate little o with 'less than'. f(x) = 0.5x² is O(x²) but not o(x²) — constant factor doesn't matter for Big O, but for little o the limit must be 0, not some constant.
Key Takeaway
Little o is strict dominance: f(n)/g(n) approaches 0 as n → ∞. Use it to claim one algorithm is asymptotically strictly better.
Little ω: The Lower Bound That Never Gets Closer
If Big Omega is 'grows at least as fast', little ω is 'grows strictly faster'. The ratio f(n)/g(n) goes to infinity. No constant multiplier lets g catch up.
Why do you care? Because when someone says 'this algorithm is ω(n log n)', they're claiming no algorithm can solve the problem faster than n log n — not even by a constant. That's the distinction between a lower bound you can hit and one you can't touch.
Example: Comparison-based sorting is ω(n) but Ω(n log n). That ω(n) says 'linear is impossible, period'. The Ω(n log n) says 'you can't beat n log n, but you can get arbitrarily close'. Little ω is stronger. It's used in hardness proofs. If your algorithm runs in ω(n²), expect a production fire when data doubles — the gap widens without limit.
LittleOmegaExample.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// io.thecodeforge — dsa tutorial// Little omega: f = 2^x grows strictly faster than g = x^3publicclassLittleOmegaExample {
staticlongf(int x) { return (long) Math.pow(2, x); }
staticlongg(int x) { return (long) Math.pow(x, 3); }
publicstaticvoidmain(String[] args) {
// For any constant c, eventually f(x) > c * g(x)double c = 0.5;
for (int x = 1; x <= 10; x++) {
boolean isLittleOmega = f(x) > c * g(x);
System.out.printf("x=%d: f(x)/g(x)=%.2f, f > %.1f*g? %b%n",
x, (double)f(x)/g(x), c, isLittleOmega);
}
}
}
Output
x=1: f(x)/g(x)=2.00, f > 0.5*g? true
x=2: f(x)/g(x)=1.33, f > 0.5*g? true
x=3: f(x)/g(x)=0.89, f > 0.5*g? true
x=4: f(x)/g(x)=1.00, f > 0.5*g? true
x=5: f(x)/g(x)=1.60, f > 0.5*g? true
x=6: f(x)/g(x)=3.56, f > 0.5*g? true
x=7: f(x)/g(x)=10.45, f > 0.5*g? true
x=8: f(x)/g(x)=42.67, f > 0.5*g? true
x=9: f(x)/g(x)=227.56, f > 0.5*g? true
x=10: f(x)/g(x)=1024.00, f > 0.5*g? true
Senior Shortcut:
For an algorithm to be ω(n), it must be Ω(n) but the lower bound can't be tightened to a constant multiple. Use this in code reviews when someone claims 'O(n) is possible' for a problem you know is ω(n).
Key Takeaway
Little ω means f(n)/g(n) → ∞. It proves a lower bound that no algorithm can beat, not even by a constant factor.
● Production incidentPOST-MORTEMseverity: high
Nested Loop in Search Took Down Production at 10M Products
Symptom
Search page timed out, database connections maxed out, CPU at 100% on application servers.
Assumption
The code worked before, so the problem must be a database bottleneck or slow network.
Root cause
Two nested loops iterating over product IDs to find duplicates — O(n²) when n=10M means 100 trillion iterations instead of 10 million.
Fix
Replaced the nested loop with a HashSet-based O(n) duplicate check. Rewrote the search to filter duplicates at query time, not after fetching all results.
Key lesson
Always complexity-analyze any code that iterates over collections that can grow unbounded.
Don't assume 'it worked yesterday' means the algorithm is safe — growth exposes hidden O(n²) fast.
Audit code paths that combine multiple data sources — they're where accidental nested loops hide.
Production debug guideSpot the complexity class before it hits production4 entries
Symptom · 01
API response time doubles when input size doubles
→
Fix
Check for nested loops or O(n) operations inside loop bodies. Look for methods like contains() on lists.
Symptom · 02
Service slows down non-linearly with dataset growth (e.g., 3x data → 9x slower)
→
Fix
High probability of O(n²) or higher. Search for nested loops over the same collection. Profile with JProfiler or Async Profiler.
Symptom · 03
Performance degrades suddenly after a data migration or feature change
→
Fix
Compare the algorithm's worst-case complexity. Check if input distribution changed (e.g., sorted vs random).
Symptom · 04
CPU usage spikes while waiting for a single request
→
Fix
Look for infinite loops, recursive calls, or hidden O(n) inside loops that cause high CPU. Use thread dumps.
★ Cheat Sheet: Spotting Complexity Problems in Code ReviewsQuick reference for identifying common complexity patterns that cause production slowdowns.
Nested for-loops over same collection−
Immediate action
Flag O(n²) and suggest converting to HashMap for lookups.
Commands
grep -c 'for.*for' $(find src -name '*.java')
Review if inner loop can be O(1) via indexing.
Fix now
Replace inner loop with a map or set lookup.
Loop containing a call to list.contains() or similar linear search+
Immediate action
Check if list is small; if not, convert to HashSet.
Commands
Check the size of the collection at runtime (add temporary logging).
Profile the method to see where time is spent.
Fix now
Replace list with a HashSet or use binary search on sorted list.
Recursive function without memoization on overlapping subproblems+
Immediate action
Check if same input is computed multiple times — leads to exponential time.
Commands
Add a map cache or array for memoization.
Measure number of calls with a counter variable.
Fix now
Apply memoization (top-down) or convert to bottom-up DP.
Loop body calls a method that itself loops over the same dataset+
Hoist the inner loop computation outside or use precomputed structures.
Complexity Class Comparison
Complexity Class
Name
Example Algorithm / Operation
n=1,000 (approx ops)
Scales to 1M items?
O(1)
Constant
Array index access, HashMap get (avg)
1
Yes — effortlessly
O(log n)
Logarithmic
Binary search, balanced BST lookup
~10
Yes — 20 steps for 1M
O(n)
Linear
Linear search, single array traversal
1,000
Yes — with care
O(n log n)
Log-linear
Merge sort, heapsort, TreeMap operations
~10,000
Yes — industry standard
O(n²)
Quadratic
Bubble sort, naive duplicate detection
1,000,000
No — too slow past ~10k
O(2ⁿ)
Exponential
Naive recursive Fibonacci, subset generation
~10³⁰⁰
Absolutely not
O(n!)
Factorial
Brute-force travelling salesman
Astronomical
Never
Key takeaways
1
Big O measures growth rate, not raw speed
O(n²) will always eventually lose to O(n log n) regardless of constants, for large enough input. Design for scale first.
2
Two sequential loops are O(n), not O(n²)
nesting is what multiplies complexity. Refactoring a nested loop into two sequential loops is often the single highest-impact optimization you can make.
3
Always specify which case you're analyzing
saying 'binary search is O(log n)' is incomplete without saying 'worst case'. Best/average/worst can differ dramatically (see Quicksort).
4
Hidden complexity lives inside loop bodies
any method call inside a loop that isn't O(1) multiplies your outer loop's complexity. Always audit what your inner calls are actually doing before signing off on a complexity claim.
5
In interviews, discuss trade-offs explicitly
'This is O(n²) worst-case but O(n) average — given our input distribution, I'll accept the worst-case because it's rare.'
Common mistakes to avoid
4 patterns
×
Treating two nested loops as always O(n²)
Symptom
Marking O(n × m) code as O(n²) when m is a small fixed constant (like iterating 12 months inside an employee loop). Performance is actually O(n), but the wrong label leads to unnecessary optimization.
Fix
Label the variables separately. If m is bounded by a constant like 12, the inner loop is O(1) and the whole thing is O(n). Only call it O(n²) when both dimensions scale with input size.
×
Ignoring hidden complexity inside loop bodies
Symptom
Labelling a loop as O(n) when the body calls a method that itself runs in O(n), making the real complexity O(n²). Example: loop calling String.contains() (O(m)) results in O(n × m).
Fix
Always drill into any method calls inside your loops. Treat method calls as black boxes until you look up or measure their complexity. If unsure, assume worst-case and verify.
×
Confusing space complexity with time complexity
Symptom
Calculating how much memory a recursive call stack uses and calling it the 'time complexity'. The two are orthogonal — a function can be O(n) time and O(n) space, or O(n) time and O(1) space (iterative version).
Fix
Track time and space separately. Recursive depth determines space complexity (O(n) stack frames for linear recursion). Time complexity is about the number of operations performed.
×
Forgetting to specify which case (best/worst/average) you're analyzing
Symptom
Saying 'binary search is O(log n)' without qualification. While it's true for average and worst, best-case is O(1) — the interviewer will expect you to know that.
Fix
Make it a habit: always prefix with 'worst-case', 'average-case', or 'best-case'. If not specified, assume the discussion is about worst-case by default.
What is the time complexity of accessing an element in a HashMap, and wh...
Q02JUNIOR
If I have a function with two nested for-loops followed by a binary sear...
Q03SENIOR
Quicksort is often described as O(n log n), but can you name a real inpu...
Q04SENIOR
Explain the difference between time complexity and space complexity. Can...
Q01 of 04SENIOR
What is the time complexity of accessing an element in a HashMap, and why isn't it always O(1)? Walk me through what happens when hash collisions occur.
ANSWER
In the average case, HashMap operations (get/put) are O(1) because the hash function distributes keys evenly across buckets. However, worst-case is O(n) when many keys hash to the same bucket, causing a linked list (or tree after threshold) traversal. Java 8+ converts long chains to trees (O(log n) worst-case). So the correct answer is 'O(1) average, O(log n) for treeified buckets under high collisions, O(n) if not'. Always qualify the case.
Q02 of 04JUNIOR
If I have a function with two nested for-loops followed by a binary search — both operating on the same array of size n — what's the overall time complexity, and how did you arrive at that answer?
ANSWER
The nested loops: O(n²). The binary search: O(log n). Since they are sequential, we add: O(n²) + O(log n). Big O keeps only the dominant term: O(n²). So overall complexity is O(n²). The binary search is irrelevant for large n. The correct answer is O(n²) because O(n²) dwarfs O(log n) as n grows.
Q03 of 04SENIOR
Quicksort is often described as O(n log n), but can you name a real input that causes it to degrade to O(n²)? How do production implementations guard against this?
ANSWER
Quicksort degrades to O(n²) when the pivot selection always picks the smallest or largest element, such as on already-sorted arrays if the pivot is always the first element. Production implementations guard against this by using median-of-three pivot selection (choose median of first, middle, last), random pivot, or introsort (which switches to heapsort if recursion depth exceeds log n). Java's Arrays.sort() uses a highly tuned dual-pivot quicksort for primitives and Timsort for objects, avoiding the worst-case entirely.
Q04 of 04SENIOR
Explain the difference between time complexity and space complexity. Can a function have better time complexity but worse space complexity than another? Give a real example.
ANSWER
Time complexity measures how the number of operations grows with input size; space complexity measures how much memory (RAM) is used. Yes, a function can be faster but more memory-hungry. Example: Fibonacci with memoization (DP) is O(n) time and O(n) space, while naive recursion is O(2ⁿ) time and O(n) space. The DP version is much faster but uses more memory for the cache. Another: merge sort is O(n log n) time and O(n) space, while heap sort is O(n log n) time and O(1) space — same time, different space.
01
What is the time complexity of accessing an element in a HashMap, and why isn't it always O(1)? Walk me through what happens when hash collisions occur.
SENIOR
02
If I have a function with two nested for-loops followed by a binary search — both operating on the same array of size n — what's the overall time complexity, and how did you arrive at that answer?
JUNIOR
03
Quicksort is often described as O(n log n), but can you name a real input that causes it to degrade to O(n²)? How do production implementations guard against this?
SENIOR
04
Explain the difference between time complexity and space complexity. Can a function have better time complexity but worse space complexity than another? Give a real example.
SENIOR
FAQ · 4 QUESTIONS
Frequently Asked Questions
01
What is the difference between time complexity and space complexity?
Time complexity measures how the number of operations your algorithm performs grows with input size. Space complexity measures how much memory (RAM) it uses as input grows. An algorithm can be fast but memory-hungry (like merge sort's O(n) extra space) or slow but memory-efficient (like bubble sort's O(1) space). In interviews and code reviews, always consider both axes separately.
Was this helpful?
02
Why do we drop constants in Big O notation?
Because constants are machine-dependent, not algorithm-dependent. An O(2n) algorithm and an O(n) algorithm have the same linear growth shape — they just run on different hardware at different speeds. Big O is a tool for comparing algorithmic structure, so we strip away anything that changes based on environment and keep only what describes how the algorithm itself scales.
Was this helpful?
03
Is O(n log n) always better than O(n²) in practice?
For large n, yes — always. But for very small arrays (typically fewer than 10-20 elements), O(n²) algorithms like insertion sort can actually be faster in practice because they have lower constant factors and better CPU cache behavior. This is why Java's Arrays.sort() uses a hybrid approach called Timsort, which switches to insertion sort for small subarrays within the merge sort process.
Was this helpful?
04
How do I calculate the time complexity of a recursive function?
Use recurrence relations. For example, merge sort recurrence: T(n) = 2T(n/2) + O(n). Solve with Master Theorem or recursion tree. Common results: Divide into two halves (2T(n/2)) + linear merge (O(n)) → O(n log n). If the recurrence is T(n) = T(n-1) + O(1), it's O(n) — linear recursion. If it's T(n) = 2T(n-1) + O(1), it's O(2ⁿ) — exponential. Write the recurrence, then solve it.