| Structure | Access | Search | Insert | Delete | When to Use |
|---|---|---|---|---|---|
| Array | O(1) | O(n) | O(n) | O(n) | Index-based access, fixed/known size, sequential iteration |
| Linked List | O(n) | O(n) | O(1) | O(1) | Frequent insert/delete at known positions, dynamic size |
| Binary Search Tree (balanced) | O(log n) | O(log n) | O(log n) | O(log n) | Sorted data with frequent updates, range queries, in-order traversal |
| Hash Map/Set | N/A | O(1) | O(1) | O(1) | Fast lookups by key, counting frequencies, detecting duplicates |
| Stack | O(1) | O(n) | O(1) | O(1) | LIFO order, undo/redo, parsing, matching brackets |
| Queue | O(1) | O(n) | O(1) | O(1) | FIFO order, BFS traversal, task scheduling, buffering |
| Heap / Priority Queue | O(1) | O(n) | O(log n) | O(log n) | Top K elements, priority queues, median finding, Dijkstra |
| Graph (Adj List) | - | O(V+E) | O(1) | O(E) | Relationships between entities, networks, paths, dependencies |
| Trie | - | O(m) | O(m) | O(m) | Prefix search, autocomplete, spell checking, word dictionaries |
| Algorithm | Best | Average | Worst | Space | Stable |
|---|---|---|---|---|---|
| Bubble Sort | O(n) | O(n²) | O(n²) | O(1) | Yes |
| Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | Yes |
| Selection Sort | O(n²) | O(n²) | O(n²) | O(1) | No |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | No |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No |
| Counting Sort | O(n + k) | O(n + k) | O(n + k) | O(k) | Yes |
| Radix Sort | O(nk) | O(nk) | O(nk) | O(n + k) | Yes |
| Algorithm | Time | Use Cases |
|---|---|---|
| BFS | O(V + E) | Shortest path (unweighted), level-order, nearest neighbor |
| DFS | O(V + E) | Path finding, cycle detection, connected components |
| Dijkstra | O((V + E) log V) | Shortest path (weighted, non-negative) |
| Topological Sort | O(V + E) | Task scheduling, dependency resolution |
| Representation | Structure | Space | Best For |
|---|---|---|---|
| Adjacency List | Map of node → list of neighbors | O(V + E) | Sparse graphs, traversals |
| Adjacency Matrix | 2D array where matrix[i][j] = edge | O(V²) | Dense graphs, edge lookups |
| Edge List | List of (source, destination) pairs | O(E) | Kruskal's algorithm, simple storage |
| Traversal | Order | Use Cases |
|---|---|---|
| Inorder | Left → Root → Right | BST sorted order, validate BST |
| Preorder | Root → Left → Right | Copy tree, serialize tree |
| Postorder | Left → Right → Root | Delete tree, calculate height |
| Level-order (BFS) | Level by level | Level sums, zigzag traversal |
| Algorithm | Time | When to Use |
|---|---|---|
| Linear Search | O(n) | Unsorted data |
| Binary Search | O(log n) | Sorted data, finding boundaries |
| Hash Lookup | O(1) avg | Exact match lookups |
| BST Search | O(log n) | Ordered data with range queries |
| Pattern | Time | Use Cases |
|---|---|---|
| Opposite Ends | O(n) | Two sum (sorted), palindrome check, container with most water |
| Same Direction | O(n) | Remove duplicates, merge sorted arrays, partition |
| Fast & Slow | O(n) | Cycle detection, find middle, linked list intersection |
| Type | Time | Use Cases |
|---|---|---|
| Fixed Window | O(n) | Max sum of k elements, string anagrams |
| Variable Window | O(n) | Longest substring without repeating, minimum window substring |
| Pattern | Examples | Key Insight |
|---|---|---|
| Linear DP | Fibonacci, climbing stairs, house robber | dp[i] depends on previous elements |
| 2D DP | Grid paths, edit distance, LCS | dp[i][j] from adjacent cells |
| Knapsack | 0/1 knapsack, coin change, subset sum | Include/exclude decision |
| Interval DP | Matrix chain, palindrome partitioning | Solve subproblems on intervals |
| Problem Type | Time | Examples |
|---|---|---|
| Permutations | O(n!) | All arrangements of elements |
| Combinations | O(2ⁿ) | Subsets, combination sum |
| Constraint Satisfaction | Varies | N-Queens, Sudoku, word search |
| Problem | Greedy Strategy |
|---|---|
| Activity Selection | Pick earliest ending activity |
| Fractional Knapsack | Pick highest value/weight ratio |
| Huffman Coding | Combine lowest frequency nodes |
| Jump Game | Always jump to max reachable |
| Operation | Code | Example | Result | Description |
|---|---|---|---|---|
| AND | & | 101 & 011 | 001 | 1 if both bits are 1 |
| OR | | | 101 | 011 | 111 | 1 if either bit is 1 |
| XOR | ^ | 101 ^ 011 | 110 | 1 if bits are different |
| NOT | ~ | ~0101 | 1010 | Flip all bits |
| Left shift | << | 101 << 1 | 1010 | Shift left, fill with 0s |
| Right shift | >> | 101 >> 1 | 10 | Shift right, drop bits |
| Get bit | (n >> i) & 1 | (101 >> 1) & 1 | 0 | Check if bit i is set |
| Set bit | n | (1 << i) | 101 | (1 << 1) | 111 | Turn on bit i |
| Clear bit | n & ~(1 << i) | 111 & ~(1 << 1) | 101 | Turn off bit i |
| Toggle bit | n ^ (1 << i) | 101 ^ (1 << 1) | 111 | Flip bit i |
| Power of 2 | n & (n-1) == 0 | 100 & 011 | 000 | True if only one bit set |
| Clear lowest bit | n & (n-1) | 110 & 101 | 100 | Removes lowest set bit |
| If you see... | Think... |
|---|---|
| "Sorted array" + "find pair/target" | Two pointers or binary search |
| "Contiguous subarray/substring" | Sliding window |
| "Top K" or "K largest/smallest" | Heap / Priority Queue |
| "All permutations/combinations" | Backtracking |
| "Shortest path" (unweighted) | BFS |
| "Shortest path" (weighted) | Dijkstra or DP |
| "Connected components" or "islands" | DFS or BFS or Union-Find |
| "Dependency order" or "prerequisites" | Topological sort |
| "Count occurrences" or "frequency" | Hash map |
| "Find duplicates" | Hash set or sorting |
| "Optimize min/max" with choices | Dynamic programming or greedy |
| "Parentheses matching" or "nested structure" | Stack |
| "Tree traversal" or "path in tree" | DFS (recursion) or BFS |
| "Prefix/autocomplete" | Trie |
| "Cycle detection" in linked list | Fast & slow pointers |
| "Range sum" or "cumulative" | Prefix sum |
| "Single number" among pairs | XOR bit manipulation |