01Array & String
0/12▶Two Pointers
Two indices scanning toward each other or in lockstep through a sorted or linear structure.
- Opposite-direction pointers on sorted arrays
- Same-direction (fast/slow) pointers for in-place partitioning
- Three-pointer extension for triplet problems
- When two pointers beats brute force O(n²)
Sliding Window
A contiguous window that expands and contracts over an array or string.
- Fixed-size window sums/averages
- Variable-size window with a shrink condition
- Window state via hash map / frequency count
- Monotonic deque for window max/min (bonus)
Cyclic Sort
Place each number at its correct index in-place when values lie in a known range like [1, n].
- Index-value mapping (value v belongs at index v-1)
- Swapping into place with a while loop per index
- Using sign-flipping or negation as a visited marker
- Spotting the 'numbers in range [1,n]' signal
Prefix Sum
Precompute running totals so any range sum becomes an O(1) subtraction.
- 1D prefix sum array construction
- Prefix sum + hash map for 'subarray sums to k' problems
- 2D prefix sum for submatrix queries
- Difference arrays for range updates
02Hashing
0/3▶Hash Map / Hash Set
Trade space for near O(1) average lookups, grouping, and duplicate detection.
- Hash map for complement lookups (Two Sum shape)
- Hash set for existence / duplicate checks
- Grouping by a computed key (sorted string, count signature)
- Hashing with custom keys (tuples, sorted arrays)
03Stack-Based
0/6▶Monotonic Stack
Keep a stack in increasing or decreasing order to answer 'next greater/smaller' queries in one pass.
- Increasing vs decreasing stack, and which problems need which
- Storing indices (not values) so distances are recoverable
- Next Greater Element pattern
- Histogram / rectangle-area style problems
Stack for Matching / Parsing
Track nested structure — brackets, expressions, or encoded strings — with a LIFO stack.
- Bracket matching with a symbol stack
- Operator/number stacks for calculator-style expression evaluation
- Decoding nested repeated patterns (e.g. k[encoded]) with a stack of (string, count)
- Min-stack trick: store running min alongside each pushed value
04Linked Lists
0/6▶Fast & Slow Pointers
Two pointers moving at different speeds through a list — the classic cycle-detection and midpoint-finding tool.
- Floyd's cycle detection (tortoise and hare)
- Finding the cycle's starting node after detection
- Using slow/fast to find the middle node in one pass
- Applying the same idea to detect cycles in functional sequences (not just lists)
In-place Reversal & Dummy Node
Rewire next pointers directly, using a dummy head node to simplify edge cases like removing the head.
- Classic full-list reversal with prev/curr/next pointers
- Reversing a sublist between two positions
- Dummy node to avoid special-casing head deletion/insertion
- Group reversal (reverse in chunks of k)
05Binary Search
0/3▶Modified Binary Search
Binary search over sorted arrays, rotated arrays, or an abstract 'answer space' where feasibility is monotonic.
- Standard binary search template (avoiding infinite loops / off-by-one)
- Searching in a rotated sorted array (find the sorted half)
- Binary search on the answer (search a value range, not an index range)
- Binary search across two sorted structures (partition-based)
06Trees
0/9▶Tree DFS (Pre/In/Postorder)
Recursive or stack-based traversal that visits nodes in a specific parent/child order.
- Preorder / inorder / postorder traversal (recursive and iterative)
- Passing state down (top-down) vs. returning state up (bottom-up)
- Using inorder traversal to validate/exploit BST ordering
- Combining two recursive calls for whole-tree properties (diameter, path sum)
Tree BFS / Level Order
Queue-based traversal that processes a tree level by level.
- Queue-based level order traversal
- Tracking level boundaries (level size snapshot)
- Zigzag / right-side-view variants
- BFS for shortest-path style tree/graph problems
Tries (Prefix Trees)
A tree where each path from the root spells out a prefix, enabling fast prefix search and autocomplete-style queries.
- Trie node structure (children map + end-of-word flag)
- Insert / search / startsWith operations
- Combining a trie with DFS/backtracking for grid word search
- Space/time tradeoffs vs. hash sets for prefix queries
07Heaps / Priority Queues
0/6▶Top-K Elements
Maintain a heap of size k to track the k largest/smallest/most-frequent items, often over a stream.
- Min-heap for 'top k largest', max-heap for 'top k smallest'
- Keeping heap size capped at k (push then pop when oversized)
- Bucket sort as an O(n) alternative for frequency-based top-k
- Heapify vs. repeated push for construction cost
K-Way Merge
Merge multiple already-sorted sequences efficiently using a heap to always pick the next-smallest candidate.
- Heap of (value, source index, position) tuples
- Advancing only the source you just popped from
- Complexity: O(n log k) instead of O(n·k)
- Applying k-way merge to matrices and multiple lists alike
08Backtracking
0/3▶Backtracking
Build candidate solutions incrementally, abandoning ('pruning') a branch as soon as it can't lead to a valid answer.
- Decision tree framing: choose → explore → un-choose
- Pruning conditions to cut off invalid branches early
- Handling duplicates (skip same-value siblings after sorting)
- Constraint propagation for grid-based backtracking (Sudoku, N-Queens)
09Graphs
0/12▶Graph DFS / BFS
Traverse a graph's nodes and edges to explore connectivity, reachability, or shortest unweighted paths.
- Adjacency list construction from edges or a grid
- DFS with a visited set for connected components
- BFS for shortest path in an unweighted graph
- Multi-source BFS (seed the queue with several starting nodes at once)
Topological Sort
Order nodes in a directed acyclic graph so every edge points from earlier to later in the ordering.
- Kahn's algorithm (BFS using in-degree counts)
- DFS-based topological sort (postorder, then reverse)
- Cycle detection as a side effect (if not all nodes get ordered, a cycle exists)
- Recognizing 'prerequisite' / dependency-ordering problem phrasing
Union-Find (Disjoint Set)
Efficiently track and merge connected components, answering 'are these in the same group?' near O(1).
- Union by rank/size and path compression
- Detecting cycles by checking if two nodes already share a root
- Counting connected components as unions succeed
- When union-find beats DFS/BFS (dynamic connectivity, incremental edges)
Shortest Path (Dijkstra / Bellman-Ford)
Find minimum-cost paths in weighted graphs, positive-only (Dijkstra) or with negative edges (Bellman-Ford).
- Dijkstra's algorithm with a min-heap of (distance, node)
- Why Dijkstra fails with negative edge weights
- Bellman-Ford's relax-all-edges V-1 times approach and negative-cycle detection
- Modeling 'at most K stops/steps' as an extra state dimension
10Dynamic Programming
0/18▶1D Dynamic Programming
State depends on a small window of previous states along a single sequence.
- Recurrence relation identification (what does dp[i] depend on?)
- Top-down memoization vs. bottom-up tabulation
- Space optimization (rolling variables instead of a full array)
- Base case and iteration direction
2D Dynamic Programming (Grid/Table)
State depends on a 2D table, typically comparing two sequences or moving through a grid.
- Grid traversal DP (dp[r][c] from dp[r-1][c] and dp[r][c-1])
- Two-sequence comparison DP (dp[i][j] over prefixes of two strings)
- Reconstructing the actual path/sequence from a filled DP table
- Space optimization to O(n) using two rolling rows
Knapsack (0/1 & Unbounded)
Choose or skip items under a capacity constraint — the archetypal subset-selection DP.
- 0/1 knapsack: each item used at most once (iterate capacity backward in 1D)
- Unbounded knapsack: items reusable (iterate capacity forward in 1D)
- Reframing subset-sum / partition problems as knapsack capacity = target
- Counting ways vs. optimizing value — same shape, different combine operator
DP on Strings
Comparing, transforming, or matching sequences character by character with overlapping subproblems.
- Palindrome DP (dp[i][j] = is substring i..j a palindrome)
- Interleaving/matching DP across two or three strings simultaneously
- Regex/wildcard matching state transitions
- Expand-around-center as an alternative to full DP tables for palindromes
DP on Trees
Combine results from child subtrees to compute an optimal value for the whole tree.
- Postorder DFS returning a tuple of states per node (e.g. 'include' vs 'exclude')
- Combining left/right subtree results at each node
- Tracking a global answer alongside a per-node return value
- Rerooting technique for 'answer for every node as root' problems (bonus)
Bitmask DP
Compress a subset of items into an integer bitmask to track 'which items have been used' as DP state.
- Representing subsets as integers and iterating with bit tricks
- dp[mask][i] state design (which items used, current position)
- Enumerating submasks of a mask when needed
- Recognizing small-n (n ≤ ~20) as a bitmask DP signal
11Greedy
0/3▶Greedy Algorithms
Make the locally optimal choice at each step and prove (or trust) that it leads to a globally optimal answer.
- Sorting by a clever key as a greedy enabler
- Exchange-argument intuition for why a greedy choice is safe
- Greedy vs. DP: recognizing when the greedy choice actually holds
- Interval-scheduling and gas-station-style greedy templates
12Intervals
0/3▶Merge Intervals
Sort intervals by start time, then sweep through merging or comparing overlaps.
- Sorting by start (or end) time as the key first step
- Overlap condition: current.start <= previous.end
- Sweep-line thinking for counting overlaps at a point in time
- Inserting a new interval into an already-sorted, non-overlapping list
13Bit Manipulation
0/3▶Bit Manipulation
Use XOR, shifts, and masks to solve problems in O(1) extra space with bitwise tricks.
- XOR properties (self-cancelling, identity) for pairing/duplicate problems
- Bit counting techniques (Brian Kernighan's algorithm: n & (n-1))
- Bit masks for representing sets/subsets
- Two's complement mechanics for negative number bit tricks
14Math & Geometry
0/3▶Math & Geometry / Simulation
Direct simulation of a described process — matrix rotation, spiral traversal, coordinate geometry.
- Matrix transposition + reversal as a rotation trick
- Layer-by-layer boundary tracking for spiral traversal
- Modular arithmetic for cyclic/overflow-safe computation
- Fast exponentiation (binary exponentiation) for power functions