Theory · Time Complexity

Time Complexity

Decidable isn’t enough — is it decidable *fast*? Big-O, complexity classes, and why the exponents matter.

Theory of Computation Basics → Interview
01

From Computable to Tractable

A problem being decidable says an algorithm exists. Complexity theory asks the sharper question: how do its time and space requirements grow with input size n? A decidable problem that takes 2ⁿ steps is, for n = 100, beyond the lifetime of the universe. “Solvable in principle” and “solvable in practice” are different universes.

We measure the asymptotic growth (big-O) because constants and hardware change; the growth rate is the property intrinsic to the problem. O(n) beats O(n²) at scale no matter whose laptop runs it.
02

The Growth Hierarchy

ClassNamen=10n=1000Feel
O(1)Constant11Hash lookup
O(log n)Logarithmic310Binary search
O(n)Linear101000One scan
O(n log n)Linearithmic33~10⁴Good sort
O(n²)Quadratic10010⁶Nested loops
O(2ⁿ)Exponential1024~10³⁰¹Brute-force subsets
O(n!)Factorial~10⁶All permutations
The cliff is between polynomial (nᵏ — scales) and exponential (2ⁿ — doesn’t). Doubling the input adds one constant of work to an O(n) algorithm but squares the work for O(2ⁿ). This polynomial/exponential line is the one P vs NP is drawn on.
03

Complexity Classes

Problems get grouped by the resources needed to solve them:

ClassMeans
PSolvable by a deterministic TM in polynomial time — “efficiently solvable”
NPA proposed solution is verifiable in polynomial time (guess + check)
PSPACESolvable in polynomial space (time may be exponential)
EXPTIMERequires exponential time — provably intractable

P ⊆ NP ⊆ PSPACE ⊆ EXPTIME. Whether any of those ⊆ is actually = is mostly open — including the million-dollar one, P vs NP, the next guide.

04

Best, Worst, Amortized

“Complexity” needs a case. Worst-case (the guarantee) is what you usually quote. Average-case assumes an input distribution (quicksort is O(n log n) average, O(n²) worst). Amortized averages over a sequence — a dynamic array’s push is O(1) amortized even though occasional resizes are O(n), because they’re rare enough to spread out.

05

Interview Questions

Why care about complexity if a problem is already decidable?

Decidable means an algorithm exists; complexity says whether it finishes before the heat death of the universe. An O(2ⁿ) decision procedure is useless at n=100. Tractability, not mere solvability, is what makes an algorithm usable.

Where’s the important line in the growth hierarchy, and why?

Between polynomial (nᵏ) and exponential (2ⁿ). Polynomial algorithms scale with hardware; exponential ones hit a wall where each added input element multiplies the work. This is the boundary P vs NP is about.

P vs NP in one sentence (setup for the next guide).

P is problems solvable in polynomial time; NP is problems whose proposed solutions are checkable in polynomial time. Whether every efficiently-checkable problem is also efficiently-solvable (P = NP) is open.

Worst vs average vs amortized complexity?

Worst-case: the guaranteed upper bound over all inputs. Average-case: expected cost over an input distribution (quicksort O(n log n) average, O(n²) worst). Amortized: average cost per operation over a sequence, spreading rare expensive operations (dynamic-array resize) across many cheap ones.

Quick Quiz

1. The critical divide for tractability is between…
2. Binary search is…
3. NP is the class of problems whose solutions are…
4. A dynamic array’s push is O(1)…
5. O(2ⁿ) at n=1000 is…