Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Cheat Sheets
Reference3 min read

Big-O Complexity

Common complexities, data-structure operation costs, and how to read constraints to guess the intended solution.

Complexity questions are guaranteed in every round: "what's the complexity?" after you code, and "can you do better?" before. This sheet is the lookup table.

The ladder

ComplexityNameFeel at n = 10⁶
O(1)constantinstant
O(log n)logarithmic~20 steps
O(n)linear10⁶ steps
O(n log n)linearithmic~2×10⁷ — fine
O(n²)quadratic10¹² — too slow
O(2ⁿ)exponentialdead past n ≈ 25
O(n!)factorialdead past n ≈ 11

Read the constraints → guess the target

The constraint block tells you the intended complexity before you write a line:

n up toIntended complexityTypical approach
10–20O(2ⁿ) or O(n!)backtracking, bitmask DP
~100O(n³)DP over pairs, Floyd–Warshall
~1,000O(n²)2-D DP, nested loops
~10⁵–10⁶O(n log n) or O(n)sort, heap, two pointers, sliding window, hash map
~10⁹+O(log n) or O(1)binary search, math

Data-structure operation costs

StructureAccessSearchInsertDelete
ArrayO(1)O(n)O(n)O(n)
Sorted arrayO(1)O(log n)O(n)O(n)
Hash map / set—O(1) avgO(1) avgO(1) avg
Linked listO(n)O(n)O(1)*O(1)*
Binary heapO(1) topO(n)O(log n)O(log n) top
BST (balanced)O(log n)O(log n)O(log n)O(log n)
Trie—O(L)O(L)O(L)

* given a reference to the node; finding it is O(n). L = key length.

Sorting

  • Comparison sorts bottom out at O(n log n) — merge sort (stable, O(n) extra), heap sort (in place), quicksort (O(n²) worst case, fast in practice).
  • Counting/bucket/radix beat it when keys are small integers or fixed-width — that's the trick behind O(n) answers to Top K Frequent Elements and Sort Characters by Frequency.

Rules of thumb when stating complexity

  • State time and space separately, and say what n (and m) refer to.
  • Recursion: complexity = number of nodes in the call tree × work per node; don't forget O(depth) stack space.
  • Amortized is a real answer: appending to a dynamic array is O(1) amortized; each element enters and leaves a monotonic stack once, so the loop is O(n) total.
  • Hash map worst case is O(n) per op — mention it, then move on with "expected O(1)".

Pitfalls

  • String concatenation in a loop is O(n²) in most languages — collect parts and join.
  • list.pop(0) / shift() are O(n); use a deque.
  • Slicing (s[i:j]) copies — inside a loop that's hidden quadratic cost.
  • "We sort first" means the total can never be reported below O(n log n).
Choosing a Pattern