The most common way complexity shows up in interview code is through loops, and the mechanical rule is straightforward once you internalize it: count how many total iterations happen, across all loops, as a function of the input size.
The most common way complexity shows up in interview code is through loops, and the mechanical rule is straightforward once you internalize it: count how many total iterations happen, across all loops, as a function of the input size.
A single loop that runs once per element of an n-element input is O(n). Two sequential loops — one after the other, each independently touching the input once — are still O(n), because you add their costs rather than multiply them: O(n) + O(n) = O(2n), and constant factors drop out, leaving O(n). This trips people up more than it should; a candidate who sees two separate loops in a row and reflexively says “O(n²)” is making a real mistake, and it’s worth training yourself out of that reflex specifically.
Nested loops are where multiplication enters. If an outer loop runs n times and, for each outer iteration, an inner loop also runs on the order of n times, the total work is proportional to n × n = n². This is the classic quadratic pattern — checking every pair of elements in a list, for instance. If the inner loop’s range shrinks as the outer index grows (a common pattern in triangle-shaped iteration, like comparing each element only to the elements after it), the total number of iterations is still proportional to n² — the sum 1 + 2 + ... + n is n(n+1)/2, and the leading term is still quadratic once constants are dropped. Three independently nested loops give you O(n³), and so on; each additional level of loop nesting over the same input generally multiplies another factor of n onto the total.
Not every loop that looks nested is actually multiplicative, though, and this is where mechanical pattern-matching fails candidates who haven’t built real intuition. Consider a two-pointer scan across an array, where an outer while loop advances one pointer and an inner while loop advances a second pointer, but crucially, neither pointer ever moves backward, and each can move forward at most n times over the life of the whole function — not per outer iteration, but in total. In that shape, even though the code has the visual structure of a nested loop, the total number of inner-loop steps across the entire run is bounded by n, not by n per outer step. That’s O(n), not O(n²), and correctly identifying that distinction is a frequent, high-value “aha” moment in interviews — you’ll see this exact reasoning applied concretely in the two-pointer and sliding-window chapters later in the book.
Continue with the book
Explore the full treatment, worked examples, and exercises in Algorithmic Edge.
Companion article for Algorithmic Edge, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.
