Skip to main content

📘 Data Structures & Algorithms (DSA) — Quick Reference Cheatsheet


1️⃣ Big-O & Complexity Analysis

Time Complexity

Measures how the running time of an algorithm grows with input size (O(1), O(n), O(log n), O(n²), etc.).

Space Complexity

Measures the extra memory used by an algorithm relative to input size.

Asymptotic Notations

Big-O (Worst case), Omega (Best case), Theta (Average case).


2️⃣ Arrays

TopicDescription
Array BasicsLinear data structure, contiguous memory
TraversalVisit each element sequentially
Insertion & DeletionMay require shifting elements
Prefix SumPreprocess cumulative sums for range queries
Kadane's AlgorithmFind maximum subarray sum
Two PointerOptimize sorted array / pair sum problems
Sliding WindowEfficient subarray/substring problems

3️⃣ Strings

TopicDescription
String ManipulationConcat, substring, reverse, compare
Pattern MatchingNaive approach
KMP AlgorithmEfficient pattern matching
Rabin-KarpHash-based searching
Z AlgorithmPattern search & substring
PalindromeForward == Backward

4️⃣ Recursion & Backtracking

TopicDescription
Recursion BasicsFunction calling itself
Tail RecursionRecursive call is last operation
BacktrackingExplore all possibilities, undo choices
Subset GenerationAll subsets of a set
PermutationsAll possible arrangements

5️⃣ Searching

AlgorithmComplexity
Linear SearchO(n)
Binary SearchO(log n)
Ternary SearchO(log₃ n)

6️⃣ Sorting

AlgorithmBestAverageWorstSpace
Bubble SortO(n)O(n²)O(n²)O(1)
Selection SortO(n²)O(n²)O(n²)O(1)
Insertion SortO(n)O(n²)O(n²)O(1)
Merge SortO(n log n)O(n log n)O(n log n)O(n)
Quick SortO(n log n)O(n log n)O(n²)O(log n)
Heap SortO(n log n)O(n log n)O(n log n)O(1)
Counting SortO(n+k)O(n+k)O(n+k)O(k)
Radix SortO(nk)O(nk)O(nk)O(n+k)

7️⃣ Linked List

TypeDescription
SinglyNodes with next pointer
DoublyNodes with next & prev pointers
CircularLast node → First node
ReverseFlip pointer direction
Floyd's AlgorithmDetect cycle with slow/fast pointer

8️⃣ Stack & Queue

StructurePropertyApplications
StackLIFOParenthesis check, expression eval, recursion
QueueFIFOBFS, scheduling
Circular QueueFixed arrayEfficient implementation
DequeBoth endsSliding window max
Priority QueuePriority-basedDijkstra, K largest
Monotonic StackIncreasing/DecreasingNext Greater Element

9️⃣ Hashing

ConceptDescription
Hash TableKey-value via hash function
ChainingLinked list per bucket
Open AddressingProbe next empty slot
HashMapO(1) avg lookup
HashSetUnique element storage

🔟 Trees

ConceptDescription
Binary Tree≤ 2 children
BSTLeft < Root < Right
InorderLeft → Root → Right
PreorderRoot → Left → Right
PostorderLeft → Right → Root
LCALowest Common Ancestor
DiameterLongest path between nodes

1️⃣1️⃣ Heap

TypePropertyUse
Min HeapParent ≤ childrenTop-K smallest
Max HeapParent ≥ childrenTop-K largest

1️⃣2️⃣ Trie

OperationComplexity
InsertO(L)
SearchO(L)
Prefix SearchO(L)

L = length of word


1️⃣3️⃣ Graphs

AlgorithmUse CaseComplexity
BFSShortest path (unweighted)O(V+E)
DFSTraversal, cycle detectO(V+E)
DijkstraShortest path (non-negative)O((V+E) log V)
Bellman-FordNegative weightsO(VE)
Floyd-WarshallAll pairs shortest pathO(V³)
Topological SortDAG orderingO(V+E)
Kruskal/PrimMSTO(E log E)

1️⃣4️⃣ Dynamic Programming

PatternClassic Problems
0/1 KnapsackKnapsack, Subset Sum
Unbounded KnapsackCoin Change, Rod Cutting
LCSLCS, Edit Distance
LISLIS, Box Stacking
Matrix DPMatrix Chain, Grid paths
DP on TreesTree diameter, Tree knapsack
DP on StringsPalindrome, Edit Distance

1️⃣5️⃣ Greedy

ProblemStrategy
Activity SelectionSort by end time
Huffman CodingMin frequency first
Fractional KnapsackBest ratio first

1️⃣6️⃣ Bit Manipulation

TrickExpression
Check even/oddn & 1
Power of twon & (n-1) == 0
Toggle bit in ^ (1 << i)
Set bit in | (1 << i)
Clear bit in & ~(1 << i)
Count set bits__builtin_popcount(n)

🎯 Interview Pattern Recognition

PatternTrigger Keywords
Two PointersSorted array, pair, triplet
Sliding WindowSubarray, substring, window
Fast & Slow PointerCycle detection, middle node
Binary SearchSorted, monotonic, minimize/maximize
BFSLevel order, shortest path
DFS/BacktrackingAll paths, permutations, combinations
DPOptimal, count ways, overlapping
GreedyLocally optimal, scheduling
HeapTop-K, median, priority
TriePrefix, dictionary, autocomplete
Union-FindConnected components, MST
Topological SortDependency ordering, DAG

🚀 Goal: Quick revision reference for interviews and problem solving. Update regularly while solving problems.