Cheat Sheet - aloalgo.com

Cheat Sheet

A comprehensive quick reference for all data structures and algorithms covered in this course. Use this page as a cheat sheet during your interview preparation.

Table of Contents

Big O Complexity Chart

Visual comparison of common time complexities. As input size grows, notice how O(n²) and O(2ⁿ) quickly become impractical while O(1) and O(log n) remain efficient.
025507510005101520Input Size (n)OperationsO(1)O(log n)O(n)O(n log n)O(n²)O(2ⁿ)

Data Structures

StructureAccessSearchInsertDeleteWhen to Use
ArrayO(1)O(n)O(n) O(n) Index-based access, fixed/known size, sequential iteration
Linked ListO(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/SetN/A O(1) O(1)O(1)Fast lookups by key, counting frequencies, detecting duplicates
StackO(1) O(n)O(1)O(1) LIFO order, undo/redo, parsing, matching brackets
QueueO(1) O(n)O(1)O(1)FIFO order, BFS traversal, task scheduling, buffering
Heap / Priority QueueO(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
m = word length, V = vertices, E = edges

Algorithms & Techniques

Sorting Algorithms

AlgorithmBestAverageWorstSpaceStable
Bubble SortO(n)O(n²)O(n²)O(1)Yes
Insertion SortO(n)O(n²)O(n²)O(1)Yes
Selection SortO(n²)O(n²)O(n²)O(1)No
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
Counting SortO(n + k)O(n + k)O(n + k)O(k)Yes
Radix SortO(nk)O(nk)O(nk)O(n + k)Yes
k = range of input values (Counting Sort) or number of digits (Radix Sort). Learn about sorting →

Graph Algorithms

AlgorithmTimeUse Cases
BFSO(V + E)Shortest path (unweighted), level-order, nearest neighbor
DFSO(V + E)Path finding, cycle detection, connected components
DijkstraO((V + E) log V)Shortest path (weighted, non-negative)
Topological SortO(V + E)Task scheduling, dependency resolution
V = vertices, E = edges. Learn graph traversals →

Graph Representations

RepresentationStructureSpaceBest For
Adjacency ListMap of node → list of neighborsO(V + E)Sparse graphs, traversals
Adjacency Matrix2D array where matrix[i][j] = edgeO(V²)Dense graphs, edge lookups
Edge ListList of (source, destination) pairsO(E)Kruskal's algorithm, simple storage

Binary Trees

TraversalOrderUse Cases
InorderLeft → Root → RightBST sorted order, validate BST
PreorderRoot → Left → RightCopy tree, serialize tree
PostorderLeft → Right → RootDelete tree, calculate height
Level-order (BFS)Level by levelLevel sums, zigzag traversal
Learn binary trees →

Searching Algorithms

AlgorithmTimeWhen to Use
Linear SearchO(n)Unsorted data
Binary SearchO(log n)Sorted data, finding boundaries
Hash LookupO(1) avgExact match lookups
BST SearchO(log n)Ordered data with range queries
Master binary search →

Two Pointer Patterns

PatternTimeUse Cases
Opposite EndsO(n)Two sum (sorted), palindrome check, container with most water
Same DirectionO(n)Remove duplicates, merge sorted arrays, partition
Fast & SlowO(n)Cycle detection, find middle, linked list intersection
Learn two pointer techniques →

Sliding Window

TypeTimeUse Cases
Fixed WindowO(n)Max sum of k elements, string anagrams
Variable WindowO(n)Longest substring without repeating, minimum window substring
Key insight: Expand right to add elements, shrink left when constraint violated. Learn more →

Dynamic Programming Patterns

PatternExamplesKey Insight
Linear DPFibonacci, climbing stairs, house robberdp[i] depends on previous elements
2D DPGrid paths, edit distance, LCSdp[i][j] from adjacent cells
Knapsack0/1 knapsack, coin change, subset sumInclude/exclude decision
Interval DPMatrix chain, palindrome partitioningSolve subproblems on intervals
Approach: (1) Define state, (2) Write recurrence, (3) Identify base cases, (4) Determine order. Learn DP →

Backtracking

Problem TypeTimeExamples
PermutationsO(n!)All arrangements of elements
CombinationsO(2ⁿ)Subsets, combination sum
Constraint SatisfactionVariesN-Queens, Sudoku, word search
Template: Make choice → Recurse → Undo choice (backtrack). Learn backtracking →

Greedy Algorithms

When greedy works: Problems with optimal substructure where local optimal leads to global optimal.
ProblemGreedy Strategy
Activity SelectionPick earliest ending activity
Fractional KnapsackPick highest value/weight ratio
Huffman CodingCombine lowest frequency nodes
Jump GameAlways jump to max reachable
Learn greedy algorithms →

Bit Manipulation

OperationCodeExampleResultDescription
AND&101 & 0110011 if both bits are 1
OR|101 | 0111111 if either bit is 1
XOR^101 ^ 0111101 if bits are different
NOT~~01011010Flip all bits
Left shift<<101 << 11010Shift left, fill with 0s
Right shift>>101 >> 110Shift right, drop bits
Get bit(n >> i) & 1(101 >> 1) & 10Check if bit i is set
Set bitn | (1 << i)101 | (1 << 1)111Turn on bit i
Clear bitn & ~(1 << i)111 & ~(1 << 1)101Turn off bit i
Toggle bitn ^ (1 << i)101 ^ (1 << 1)111Flip bit i
Power of 2n & (n-1) == 0100 & 011000True if only one bit set
Clear lowest bitn & (n-1)110 & 101100Removes lowest set bit
Learn bit manipulation →

Pattern Recognition Guide

Use these hints to identify which technique to apply based on problem characteristics.
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 choicesDynamic 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 listFast & slow pointers
"Range sum" or "cumulative"Prefix sum
"Single number" among pairsXOR bit manipulation
Was this helpful?
© 2026 aloalgo.com. All rights reserved.