skip to content

Complexity Analysis

How to measure and compare the efficiency of code before ever running it — the analysis toolkit every other DSA topic leans on. Interviewers expect a time and space estimate for every solution you propose, so this vocabulary shows up in nearly every coding round.

part ofData structures & algorithmsoverview, primer and where to startread it →
on this pageshow

explore

questions

page 2 of 2

Why is it a misconception that Big-O notation means worst-case running time?

level: middleimportance: should knowfreq 50%

basics

~20 s

Big-O bounds the growth of a function; the case — best, worst, average — chooses which function you bound. They are independent axes: a worst case can have O, Theta, and Omega bounds, and a best case can meaningfully be Omega(n).

open as a page

For a randomized routing algorithm, how does expected-case complexity differ from average-case?

level: middleimportance: should knowfreq 45%

basics

~20 s

Expected case averages over the algorithm's own random choices, so it holds for every input. Average case averages over an assumed input distribution and collapses if that assumption is wrong. Randomization moves the uncertainty from the data to the coin flips.

open as a page

Why can't O(E+S) over E events and S servers be simplified to O(E)?

level: middleimportance: should knowfreq 52%

basics

~20 s

E and S are independent inputs, so neither provably dominates the other. Lower-order terms may only be dropped within a single variable. O(E+S) becomes O(E) only when the problem guarantees S is bounded by a multiple of E.

open as a page

How do you decide whether brute-forcing all n! orderings or all 2^n subsets is feasible?

level: middleimportance: should knowfreq 45%

basics

~20 s

Do the arithmetic instead of labelling both 'exponential'. At n=12 there are 4,096 subsets but 479 million orderings — a gap of five orders of magnitude. Subsets stay tractable to roughly n=25-30; orderings die around n=12-14.

open as a page

Why can't the master theorem solve T(n) = T(n-1) + O(n), and what does?

level: middleimportance: should knowfreq 46%

basics

~20 s

The master theorem covers only T(n) = a·T(n/b) + f(n), where a subproblem is n divided by a constant b above 1. Subtracting one leaves no such b, so no case applies. Summing the per-call work gives Θ(n^2).

open as a page

When you unroll T(n)=2T(n/2)+cn by substitution, what stops the expansion and sets the log factor?

level: middleimportance: should knowfreq 40%

basics

~20 s

The base case stops it. After k substitutions the expression reads 2^k·T(n/2^k) + k·cn; you stop when n/2^k hits the base-case size, at k = log2 n. That is where the accumulated cn per round becomes cn log n.

open as a page

When does memoizing a pricing calculation stop paying for the memory it consumes?

level: middleimportance: should knowfreq 50%

basics

~20 s

Memoization pays only when the same key is requested repeatedly. Its memory is distinct keys reached times bytes per entry, so a wide key space visited once each — every product-and-region pair in a nightly sweep — costs everything and saves nothing.

open as a page

Your request path uses an amortized O(1) structure but p99 spikes — how do you explain and fix it?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Nothing is violated: an amortized O(1) structure is allowed to let one call absorb work that many cheap calls prepaid, and that call lands in some request's p99. Fix the tail by spreading the bulk step out or moving it off the request path.

open as a page

Amortized O(1) append still misses a 2 ms deadline on a sensor ingestion buffer — why?

level: seniorimportance: should knowfreq 45%

basics

~20 s

An amortized bound covers the total cost of a sequence, not any one call. The append that fills the buffer copies every element, costing Theta(n), and that single call blows a per-operation deadline no matter how good the average is.

open as a page

A text pass copies the rest of a large document with a slice each iteration — why does throughput collapse?

level: seniorimportance: should knowfreq 45%

basics

~20 s

A slice that copies costs time proportional to its length, so taking the remaining suffix on every iteration copies a quadratic total of characters — O(n^2) time and allocation. Passing start and end offsets instead keeps the pass linear.

open as a page

A left and right index converge over a timestamp log with swaps inside — why is that loop O(n), not O(n^2)?

level: seniorimportance: should knowfreq 46%

basics

~20 s

Every iteration moves at least one index, each index moves in only one direction, and together they can cover at most n positions before meeting. So the loop runs at most n times, whatever the body does per pass.

open as a page

What lookup guarantee should a hash-based telemetry dedupe service promise in its SLA?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Promise a measured percentile, not an asymptotic class. A hash lookup is expected O(1) and worst-case O(n) for a single operation, so the SLA states a p99 latency at a named key volume and load, with the distribution assumption written down.

open as a page

A downsampling pass costs 5n log n + 200n steps — is O(n log n) an honest label?

level: seniorimportance: should knowfreq 44%

basics

~20 s

Formally yes: for large enough n the 5n log n term dominates. But 200n is the larger term for every n below roughly 10^12, so the label predicts the growth shape, not the runtime anyone will measure.

open as a page

A sublinear exact duplicate detector over an n-packet stream is proposed — how do you evaluate it?

level: seniorimportance: should knowfreq 46%

basics

~20 s

An algorithm that skips even one packet is defeated by an adversary who plants the duplicate there, so exact detection over a raw n-packet stream is Omega(n). A sublinear claim means work moved, the guarantee weakened, or n means something smaller.

open as a page

Memoizing an exponential recursion doesn't always make it polynomial — how do you tell in advance?

level: seniorimportance: should knowfreq 50%

basics

~20 s

Count the distinct reachable states the cache key can take, then multiply by the work each state does outside recursion. A key holding a subset of n items has 2^n values — caching helps enormously and still leaves you exponential.

open as a page

Why does T(n)=2T(n/2)+O(n^2) total Θ(n^2) rather than Θ(n^2 log n)?

level: seniorimportance: should knowfreq 44%

basics

~20 s

Per-level work halves as you descend: n^2, then n^2/2, then n^2/4. The series sums to under 2n^2, so the root call dominates and the total is Θ(n^2). Multiplying per-call work by depth only works when levels cost the same.

open as a page

How would you size the seen-set for deduplicating a billion-row clickstream, and what changes when it exceeds RAM?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Size it as distinct keys times realistic bytes per entry, not raw key bytes: a billion 16-byte keys lands in tens of gigabytes. Above RAM, partition by key hash, order the data so duplicates meet, or accept an approximate filter.

open as a page

A breadth-first walk of a shallow org chart with 100,000 direct reports exhausts memory, but depth-first does not. Why?

level: seniorimportance: should knowfreq 50%

basics

~20 s

Breadth-first peak memory is the widest level, depth-first peak memory is the height. A wide, shallow org chart has a frontier of 100,000 nodes but a depth of two or three, so the queue explodes while the recursion barely nests.

open as a page

With bookings capped at 50, how do you justify shipping the O(n^2) check over the O(n log n) one?

level: principalimportance: should knowfreq 36%

basics

~20 s

At n = 50 the quadratic pass is about 2,500 operations and the alternative about 280 — both invisible, so runtime cannot decide. Correctness risk and maintenance cost do; ship the simple version and enforce the cap.

open as a page

When do you accept a hidden-quadratic call inside a loop instead of paying for the rewrite?

level: principalimportance: should knowfreq 35%

basics

~20 s

Accept it when an enforced bound caps the input, the measured cost at ten times today's volume still fits the budget, and the simpler code is clearer. Then encode that bound as validation plus a test that fails when it is exceeded.

open as a page

Would you replace an O(n^2) step with an O(n log n) one when a contract caps input at 5,000 records?

level: principalimportance: should knowfreq 36%

basics

~20 s

Probably not on performance grounds alone: at 5,000 records the quadratic step is about 2.5x10^7 element operations, typically tens of milliseconds. Decide on exposure instead — how firm the cap is, what happens when it breaks, and who maintains the harder code.

open as a page

Your ingest budget forbids touching every packet, yet exact detection is Omega(n) — which guarantee do you weaken?

level: principalimportance: should knowfreq 38%

basics

~20 s

A proven lower bound is not the negotiable part, so the only lever is the problem statement: weaken exactness, shrink the input covered, or move the cost off the latency path. Pick the relaxation whose failure mode the business can absorb.

open as a page

How do you decide whether a recursive parser over user-supplied nested data must be rewritten iteratively?

level: principalimportance: should knowfreq 36%

basics

~20 s

Decide by who controls the depth. If the nesting comes from outside your trust boundary it is unbounded, and the fixed stack ceiling becomes a remote crash switch — so cap the accepted depth or keep pending work off the call stack.

open as a page

Why does finding the maximum of n tournament seeds require at least n-1 comparisons?

level: middleimportance: nice to knowfreq 30%

basics

~20 s

Every seed except the winner must lose a comparison before it can be ruled out, and one comparison rules out at most one seed. With n-1 to eliminate, no correct algorithm uses fewer than n-1 comparisons.

open as a page

What does a running-time guarantee of o(n log n) promise that O(n log n) does not, and what may a caller actually rely on?

level: seniorimportance: nice to knowfreq 15%

basics

~20 s

Little-o is strictly stronger: o(n log n) means the running time grows strictly slower than n log n — their ratio tends to zero — while O(n log n) permits growth exactly proportional to n log n. Neither promises anything about constants or small inputs.

open as a page

For an arbitrary-precision multiply with T(n)=3T(n/2)+O(n), what does the master theorem give and when does it actually beat the quadratic method?

level: seniorimportance: nice to knowfreq 30%

basics

~20 s

It solves to Θ(n^(log_2 3)), about Θ(n^1.585), by case 1: the watershed beats the linear combine, so the leaves dominate. It outruns the quadratic schoolbook method only above a crossover size, because its constants, temporaries and call overhead are far larger.

open as a page

showing 31–56 of 56