LeetCode Patterns Cheat Sheet (with Extended Explanations)
1. Two Pointers
Idea: Use two indices that move from different directions (start/end) or at different speeds to efficiently process data.
Common Problems: Reverse array, pair sum, remove duplicates, container with most water, palindrome check.
Explanation:
The two-pointer pattern eliminates the need for nested loops by allowing simultaneous traversal. It’s widely useful for sorted arrays, string manipulations, and pair-based problems.
In real-world scenarios, it can optimize algorithms where you compare or merge data streams (like merging sorted lists, filtering data, or processing large sorted datasets efficiently).
2. Sliding Window
Idea: Keep a moving “window” (subarray or substring) that expands or shrinks as you move through the array or string.
Common Problems: Longest substring without repeating characters, maximum subarray sum, minimum window substring.
Explanation:
This pattern helps manage contiguous data efficiently by maintaining running information instead of recalculating every time.
It’s useful in any context where you analyze time-series data, network traffic, real-time averages, or streaming windows—for example, computing moving averages or detecting anomalies in data streams.
3. HashMap / Frequency Counting
Idea: Use a dictionary or hash map to store frequencies, indices, or complementary values for O(1) lookups.
Common Problems: Two sum, subarray sum equals K, anagrams, majority element, first unique character.
Explanation:
Hash maps allow fast lookups and updates, turning O(n²) problems into O(n). They’re crucial for handling data where relationships, counts, or associations matter.
In real-world code, this is used for caching, indexing, counting occurrences, or tracking visited items in large datasets efficiently.
4. Fast & Slow Pointers (Linked List)
Idea: Use one pointer moving twice as fast as another to detect cycles or find middle points efficiently.
Common Problems: Detect cycle, find middle node, remove N-th node from end.
Explanation:
A classic linked list pattern that avoids extra memory while maintaining O(n) performance.
Beyond lists, this concept can generalize to detecting cycles in graphs, monitoring repeated states, or optimizing stream processing where you must track two different traversal speeds or phases.
5. Monotonic Stack
Idea: Maintain a stack in increasing or decreasing order to process “next greater” or “next smaller” relationships efficiently.
Common Problems: Next greater element, largest rectangle in histogram, daily temperatures, stock span problem.
Explanation:
Monotonic stacks help find relationships between elements without scanning everything repeatedly.
This approach generalizes to trend analysis (like stock price monitoring), processing dependencies, or analyzing peaks and valleys in data sequences, making it very valuable in real-world algorithmic problems.
6. DFS / BFS (Tree & Graph)
Idea: DFS explores depth-first (recursively) while BFS explores level by level (iteratively).
Common Problems: Connected components, path finding, binary tree traversal, island counting.
Explanation:
DFS and BFS form the foundation for exploring any structured data — trees, graphs, grids, or networks.
They extend naturally to web crawlers, game pathfinding, AI search algorithms, social network analysis, and network routing problems.
7. Dynamic Programming (DP)
Idea: Break a problem into smaller overlapping subproblems and store results to avoid redundant computation.
Common Problems: Fibonacci, climbing stairs, knapsack, grid paths, edit distance, longest increasing subsequence.
Explanation:
DP builds on recursion with memoization, converting exponential-time problems into polynomial-time solutions.
Beyond LeetCode, DP underlies optimization problems, resource allocation, path planning, and text processing algorithms (like spell checkers or DNA alignment). It’s the go-to strategy for problems involving “optimal choices.”
8. Binary Search
Idea: Repeatedly divide the search space in half to efficiently find a target or threshold.
Common Problems: Search in sorted array, search insert position, minimum in rotated array, first/last occurrence.
Explanation:
Binary search drastically reduces search time in sorted or monotonic data (O(log n)).
In practice, it’s used in database indexing, optimization problems (“find smallest X that satisfies condition”), calibration systems, or tuning parameters when results behave monotonically with respect to an input variable.
9. Greedy
Idea: Make the locally optimal choice at each step, hoping it leads to a global optimum.
Common Problems: Interval scheduling, jump game, coin change (greedy version), gas station, activity selection.
Explanation:
Greedy algorithms work when local choices yield globally optimal results — often faster and simpler than DP.
They’re applied in task scheduling, resource allocation, network routing, minimum spanning tree, and compression algorithms (like Huffman encoding).
The trick is knowing when greedy logic is provably correct for the given constraints.
10. Bit Manipulation
Idea: Use binary operations (AND, OR, XOR, shifts, masks) to encode or analyze data efficiently.
Common Problems: Single number, counting bits, subsets generation, power of two, missing number.
Explanation:
Bit manipulation enables compact and high-performance computations.
It’s essential in low-level programming, security algorithms, data compression, and performance-critical systems.
On LeetCode, it helps handle problems involving parity, state compression in DP, or subset enumeration efficiently in O(2ⁿ) space.