Skip to content
ΣDSA Patterns
Menu
Language

Complexity

Complexity & Big-O

A practical primer for coding interviews: what Big-O means, common bounds, how to analyze loops, and how space complexity fits in.

What Big-O measures

Time complexity describes how the work an algorithm does scales as the input size n grows. In interviews you almost always quote the worst-case Big-O; average case comes up for structures like hash maps.

Big-O drops constant factors and lower-order terms: 3n² + 100n + 5 is O(n²). The goal is a fast answer to “does this approach still work as n grows?”

Common bounds

Big-ONameTypical exampleWhen you see it
O(1)ConstantArray index, hash map avg get/putWork independent of input size
O(log n)LogarithmicBinary search, balanced BST lookupHalve the search space each step
O(n)LinearSingle pass, scan an arrayTouch each element once
O(n log n)LinearithmicGood sorts (merge/heap), many sort-then-scanSorting or divide-and-conquer merge
O(n²)QuadraticNested double loop, naive pairsEvery pair / every i,j
O(2ⁿ)ExponentialFull subset enumeration, naive recursionInclude/exclude each element
O(n!)FactorialAll permutationsOrderings (n must be tiny)

How to count loops

Space complexity

How much extra memory beyond the input? Output arrays are sometimes counted separately, clarify in the interview. Recursion depth d usually costs O(d) stack space.

Rough table by structure

StructureAccessSearchInsertNote
Array / listO(1)O(n)O(n)**Append may be amortized O(1)
Hash map / set-O(1) avgO(1) avgO(n) worst with bad hashing
Sorted arrayO(1)O(log n)O(n)Binary search enabled
Stack / queueO(1) endsO(n)O(1)End operations only
Min/max heapO(1) minO(n)O(log n)Top-K and priority
Balanced BST-O(log n)O(log n)Ordered traversal

Interview tips

Next step

Move into foundations on the roadmap, browse patterns, or open resources for platforms, books, and references.