⚡ Complexity Cheatsheet

Quick Reference

Dùng file này như một “cheat sheet” trong interview prep. In ra hoặc pin lên màn hình. ← Roadmap-FAANG


Constraint → Algorithm Map

Input Size Max ComplexityAlgorithms
Permutations, brute force
Bitmask DP with transitions
Subset enumeration, Bitmask DP
Floyd-Warshall, 3D DP
Matrix chain multiplication DP
Bubble/Insertion sort, DP
String matching naive, 2D grid BFS
Merge sort, Heap, Segment Tree, Dijkstra
Two pointers, Sliding window, Linear sort
Binary search, Fast exponentiation
Binary search on answer + check

Data Structure Complexities

Array / Dynamic Array

OperationAverageWorst
Access
Search
Insert (end) amortized
Insert (middle)
Delete

Linked List

OperationAverageNotes
AccessNo random access
Search
Insert (head)
Insert (tail with tail pointer)
Insert (middle — after finding node)Finding is

Hash Table

OperationAverageWorst (hash collision)
Search
Insert
Delete

Hash Map trong JS/TS

Map<K,V>Set<T> đều average. Tránh dùng plain object {} cho numeric keys vì key order behavior khác.

Binary Search Tree (Balanced — AVL/Red-Black)

OperationAverageWorst
Search
Insert
Delete

Heap (Binary Heap)

OperationComplexity
Build heap
Insert
Get min/max
Extract min/max
Decrease key

Graph

AlgorithmTimeSpaceUse Case
BFSShortest path (unweighted)
DFSCycle detection, topo sort
DijkstraShortest path (non-negative weights)
Bellman-FordShortest path (negative weights)
Floyd-WarshallAll-pairs shortest path
Prim’s MSTMinimum spanning tree
Kruskal’s MSTMinimum spanning tree

Sorting

AlgorithmBestAverageWorstSpaceStable?
Merge Sort
Quick Sort
Heap Sort
Tim Sort (JS default)
Counting Sort
Radix Sort

Recursion & DP Space Complexity

TypeSpace
Recursion depth — thường là hoặc
Memoization (top-down DP) + call stack
Tabulation (bottom-up DP) — no call stack overhead
Space-optimized DP (1D rolling)

String Algorithms

AlgorithmTimeUse Case
Naive pattern matchSimple, small inputs
KMPPattern matching
Rabin-Karp (Rolling Hash) avgMultiple pattern search
Z-algorithmPattern matching
Trie insert/searchPrefix search, = word length
Suffix Array buildAdvanced string problems

Master Theorem (Divide & Conquer)

Với recurrence:

ConditionResult

Common examples:

  • Merge Sort:
  • Binary Search:

Roadmap-FAANG | 01-Complexity-and-Array