Skip to content
Reference

Cheat Sheets

Quick reference guides for every topic. Perfect for last-minute revision before your interview.

Quick Reference: Time Complexities

Data StructureAccessSearchInsertDelete
ArrayO(1)O(n)O(n)O(n)
StackO(n)O(n)O(1)O(1)
QueueO(n)O(n)O(1)O(1)
Linked ListO(n)O(n)O(1)O(1)
Hash MapN/AO(1)O(1)O(1)
BST (Balanced)O(log n)O(log n)O(log n)O(log n)
HeapO(1)O(n)O(log n)O(log n)
TrieN/AO(m)O(m)O(m)
Segment TreeO(log n)O(log n)O(log n)O(log n)

* m = length of key, n = number of elements

Sorting Algorithms

AlgorithmBestAverageWorstSpaceStable
Bubble SortO(n)O(n²)O(n²)O(1)Yes
Insertion SortO(n)O(n²)O(n²)O(1)Yes
Merge SortO(n log n)O(n log n)O(n log n)O(n)Yes
Quick SortO(n log n)O(n log n)O(n²)O(log n)No
Heap SortO(n log n)O(n log n)O(n log n)O(1)No
Tim SortO(n)O(n log n)O(n log n)O(n)Yes

Algorithm Pattern Decision Tree

Is the data sorted?

Yes →Binary Search, Two Pointers, Sliding Window
No →

Need fast lookups? → HashMap

Need to find pairs? → Two Pointers (sort first) or HashMap

Need subarray/substring? → Sliding Window

Need shortest path? → BFS (unweighted) or Dijkstra (weighted)

Need all permutations? → Backtracking

Need optimal value? → DP or Greedy

Need connected components? → Union-Find or DFS

DP vs Greedy?

Does local optimal always lead to global optimal? → Greedy

Are subproblems overlapping? → DP (Memoization or Tabulation)

Need to consider all possibilities? → Backtracking