Nested Loop Time Complexity | Java Fresher Interview
Fast recall
Quick revision
Core answer
j from i), sum the work: n + (n-1) + ... + 1, which is still O(n^2). If the inner loop runs a fixed number of times regardless of n, the nesting is just O(n).Whether you can analyze nested loops precisely, including triangular and fixed-inner cases
Blindly calling every nested loop O(n^2) without checking the inner loop's actual range
Show the triangular sum n(n-1)/2 still reduces to O(n^2), and spot the fixed-inner O(n) case
Interview-ready guide
Interview answer
To analyze a nested loop, my basic rule is to multiply the number of times each loop runs.
If the outer loop runs n times and the inner loop also runs n times, that's n times n, so O of n squared. But I'm careful not to just count the nesting depth, because the inner loop's actual range is what matters. There are a few cases I distinguish.
The straightforward case is both loops running fully from zero to n, which is clearly O of n squared. Then there's the triangular case, where the inner loop starts at the outer index, so for j from i to n. Here the inner loop runs n times, then n minus one, down to one, and that sum is n times n plus one over two.
After I drop the constant, that's still O of n squared, even though it does about half the work of the full square. If the inner loop runs a fixed number of times, say always five, its work does not grow with n. Then the total is n times five, which is just O of n, linear, even though it looks like a nested loop.
And if the two loops use different sizes, say n and m, it's O of n times m, not O of n squared. There's also the case where the inner loop halves a value each step, which is log n, making the whole thing O of n log n.
So my approach is always to read the inner loop's bound carefully and ask whether it depends on n, on the outer index, or is just a constant.
Nested Loop Time Complexity | Java Fresher Interview
Direct Answer
Multiply the iteration counts of the loops. Two independent nested loops each running n times give O(n^2). But if the inner loop's range depends on the outer index (e.g. j from i), sum the work: n + (n-1) + ... + 1, which is still O(n^2). If the inner loop runs a fixed number of times regardless of n, the nesting is just O(n).
Code Example
// Case 1: O(n^2) - both loops full n
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) work(); // n * n
// Case 2: O(n^2) - triangular, inner starts at i
for (int i = 0; i < n; i++)
for (int j = i; j < n; j++) work(); // n + (n-1) + ... = n(n+1)/2
// Case 3: O(n) - inner runs a FIXED 5 times, not n
for (int i = 0; i < n; i++)
for (int j = 0; j < 5; j++) work(); // n * 5 = O(n)
Always read the inner loop's bound: j < n differs from j < 5.
Common Mistakes & Pitfalls
- Auto O(n^2): not every nested loop is quadratic - check whether the inner bound depends on n.
- Ignoring triangular sums:
j = ilooks cheaper butn(n+1)/2is still O(n^2). - Missing fixed-inner O(n): a constant inner loop keeps it linear.
- Different variables:
for i<nthenfor j<mis O(n*m), not O(n^2).
Check your understanding
Answer these without looking back at the article.
- What is the complexity of bubble sort?
- How do you analyze recursive algorithms?
- What does O(n log n) look like in code?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.