Top 16 LeetCode Patterns: Interactive Visualizer
Stop solving random LeetCode problems. Master the 16 underlying patterns behind the majority of coding interview questions at Google, Meta, Amazon, and other top tech companies.
16
Coding Patterns107
Curated ProblemsLifetime
AccessFree
Starter ProblemsPatterns You'll Master
Two Pointers & Sliding Window
Fast & Slow Pointers (Floyd's)
Merge Intervals & Scheduling
Binary Search Variations
BFS & DFS on Trees & Graphs
Dynamic Programming (1D, 2D, Knapsack)
Backtracking & Subsets
Top K Elements & Heaps
Topological Sort
Choose a Pattern to Start
The 16 coding interview patterns, explained
Interview questions are rarely new. They are variations on a small number of underlying techniques, and the skill being tested is recognising which one a problem is asking for. Each pattern below covers what the technique does, how to spot it in a question you have not seen, and what it costs — followed by the problems where you can watch it run.
Two Pointers
Two indices walk the same array, usually from opposite ends toward each other or at different speeds. Each step, one pointer moves based on a comparison, so the pair sweeps the whole search space in a single pass instead of checking every combination.
The array is sorted, or sorting it does not lose information, and the question asks for a pair or triplet meeting some condition. "Find two numbers that sum to X", "the container holding the most water", "remove duplicates in place" are all this pattern. If your first instinct is a nested loop over the same array, check whether two pointers collapses it.
O(n) time and O(1) extra space, against O(n²) for the nested-loop version. When sorting is required first, the sort dominates at O(n log n).
Underlying concept: Two Pointers in the DSA visualizer curriculum
8 problems in this pattern
Sliding Window
A window defined by a left and right index moves across the data. The right edge extends to include new elements; the left edge advances only when the window breaks a constraint. Running totals update incrementally rather than being recomputed, so each element is visited a constant number of times.
The problem asks for the longest, shortest, or best contiguous subarray or substring satisfying a condition. The word "contiguous" is the tell, as is a fixed window size k. "Longest substring without repeating characters" and "maximum sum subarray of size k" are the canonical forms.
O(n) time — each index enters and leaves the window at most once — against O(n²) or O(n³) for recomputing every subarray.
Underlying concept: Sliding Window in the DSA visualizer curriculum
6 problems in this pattern
Intervals
Intervals are sorted by one endpoint, then swept in order while tracking what is currently open. Almost every interval problem reduces to sorting correctly and then handling one comparison between the current interval and the previous one.
The input is a list of start/end pairs — meetings, bookings, ranges — and the question involves merging, overlap, insertion or counting concurrency. If the input looks like [[1,3],[2,6],[8,10]], this is the pattern.
O(n log n), dominated by the sort; the sweep itself is O(n). Choosing the wrong sort key is the most common failure, and it is usually end time rather than start time.
7 problems in this pattern
Linked List
Pointer manipulation with no random access. Almost every linked list technique is one of three moves: run two pointers at different speeds, reverse a section in place, or use a dummy head so the first node needs no special case.
The input is a list rather than an array, and the question asks about cycles, the nth node from the end, reordering, or comparing the list against itself. The giveaway constraint is "do it in one pass" or "use O(1) extra space" — both rule out copying into an array.
O(n) time and O(1) extra space for the pointer techniques. Copying into an array also gives O(n) time but costs O(n) space, which is usually the thing being tested.
Underlying concept: Linked List in the DSA visualizer curriculum
6 problems in this pattern
Stack
A stack holds elements whose fate is still undecided. The monotonic variant keeps its contents in sorted order by popping anything that violates that order as new elements arrive — and each pop is the moment an earlier element gets its answer.
The problem asks for the next greater or smaller element, needs matching pairs validated (brackets, tags), or involves undoing and revisiting recent state. Histogram and temperature problems are monotonic stack in disguise.
O(n) time, because every element is pushed once and popped at most once, against O(n²) for scanning forward from each position.
Underlying concept: Stack in the DSA visualizer curriculum
9 problems in this pattern
Binary Search
Halve the search space on every comparison. The powerful form in interviews is not searching a sorted array but binary searching the answer itself: guess a value, ask a yes/no feasibility question, and discard half the range of possible answers.
The data is sorted or rotated-sorted, or — more often — the answer lies in a numeric range and there is a monotonic feasibility check ("if capacity C works, C+1 also works"). "Minimum capacity to ship packages in D days" is binary search on the answer.
O(log n) per search. Binary searching an answer range costs O(log(range) × cost of the feasibility check).
Underlying concept: Binary Search in the DSA visualizer curriculum
3 problems in this pattern
Heap / Priority Queue
A heap keeps the smallest or largest element instantly reachable while allowing insertions and removals in logarithmic time. Keeping a heap of size k gives you the top k of a stream without ever sorting the whole input.
The question says "top k", "k largest", "k closest", "median of a stream", or requires repeatedly taking the current minimum — which is what makes it the engine inside Dijkstra and k-way merges.
O(n log k) for a top-k pass against O(n log n) for a full sort. Peeking is O(1); push and pop are O(log n).
Underlying concept: Heap / Priority Queue in the DSA visualizer curriculum
5 problems in this pattern
Depth-First Search
Follow one path as far as it goes, then back up and try the next. On trees this gives the preorder, inorder and postorder traversals; on graphs it needs a visited set to avoid revisiting nodes.
You need to explore every node, test whether a path exists, work with tree structure, or compute something that depends on a node’s children before the node itself — the postorder shape behind most tree DP.
O(V + E) time. Space is O(h) for the recursion stack, where h is depth — which is O(n) in the worst case for a skewed tree.
Underlying concept: Depth-First Search in the DSA visualizer curriculum
21 problems in this pattern
- Introduction to DFSFree
- DFS FundamentalsFree
- Return Values in DFS
- Maximum Depth of Binary Tree
- Path Sum
- Passing Values Down (State)
- Validate Binary Search Tree
- Binary Tree Tilt
- Diameter of Binary Tree
- Path Sum II
- Longest Univalue Path
- Graphs Overview
- Graph Representation: Adjacency List
- Clone Graph
- Graph Valid Tree
- Matrices as Graphs
- Flood Fill
- Types of DFS (Tree Traversals)
- Number of Islands
- Surrounded Regions
- Pacific Atlantic Water FlowFree
Breadth-First Search
Explore level by level using a queue: all nodes at distance 1, then all at distance 2, and so on. Because levels are visited in order, the first time a node is reached is guaranteed to be by a shortest path.
The question asks for the shortest path or minimum number of steps in an unweighted graph or grid, or for something computed per level ("level order traversal", "rightmost node of each row"). "Minimum number of moves" almost always means BFS.
O(V + E) time, O(V) space for the queue and visited set. On a grid that is O(rows × cols).
Underlying concept: Breadth-First Search in the DSA visualizer curriculum
11 problems in this pattern
Backtracking
Build a candidate solution one choice at a time. Recurse after each choice, and if the branch cannot lead to a valid answer, undo the choice and try the next. The undo step is what separates backtracking from plain recursion.
The problem asks for all permutations, all subsets, all combinations, or all ways of doing something — or it is a constraint puzzle like N-Queens or Sudoku. "Find every…" means backtracking.
Exponential by nature — O(2ⁿ) for subsets, O(n!) for permutations. Pruning invalid branches early is the only meaningful optimisation.
Underlying concept: Backtracking in the DSA visualizer curriculum
6 problems in this pattern
Graphs (Topological Sort)
Topological sort orders the nodes of a directed acyclic graph so every edge points forward. It is produced either by repeatedly removing nodes with no remaining prerequisites (Kahn’s algorithm) or by reversing a DFS finish order.
Tasks have prerequisites and you need a valid order, or you need to detect whether the dependencies contain a cycle. Course schedules, build orders and dependency resolution are all this.
O(V + E). If the algorithm finishes having emitted fewer than V nodes, the graph contains a cycle — which is how cycle detection falls out for free.
Underlying concept: Graphs (Topological Sort) in the DSA visualizer curriculum
3 problems in this pattern
Dynamic Programming
Solve each distinct subproblem once and reuse the answer. Memoization caches results on top of the natural recursion; tabulation fills a table bottom-up. Both depend on finding a recurrence that expresses a problem in terms of smaller versions of itself.
The problem asks for a count of ways, a minimum or maximum over choices, or whether something is achievable — and a brute-force recursion would solve the same subproblem repeatedly. Overlapping subproblems plus optimal substructure is the signature.
Usually O(states × transitions). A 1D DP over n items is O(n); knapsack and most string DP are O(n × m). Space often reduces to one or two rows.
Underlying concept: Dynamic Programming in the DSA visualizer curriculum
9 problems in this pattern
Greedy Algorithms
Take the best-looking option at each step and never reconsider. Greedy produces short, fast solutions — but only for problems where the locally optimal choice is provably globally optimal, which is the part that has to be argued rather than assumed.
The problem asks for a maximum or minimum and there is a natural ordering that makes one choice obviously safest — earliest finishing time, largest value first. If a counterexample comes to mind quickly, the problem is dynamic programming instead.
Typically O(n log n) for the sort plus O(n) for a single pass.
Underlying concept: Greedy Algorithms in the DSA visualizer curriculum
4 problems in this pattern
Trie
A tree where each edge is a character, so words sharing a prefix share a path. Lookup cost depends on the length of the word rather than the number of words stored.
The problem involves prefixes — autocomplete, prefix search, word dictionaries — or repeatedly checks membership of many strings. Grid word-search problems use a trie to prune impossible branches early.
O(L) per insert or search, where L is word length, independent of how many words are stored. Space is O(total characters) in the worst case.
Underlying concept: Trie in the DSA visualizer curriculum
3 problems in this pattern
Prefix Sum
Precompute cumulative totals once, then answer any range-sum query as the difference of two prefix values. Pairing prefix sums with a hash map turns "count subarrays summing to k" into a single pass.
The problem asks about sums or counts over many subarrays or ranges, especially with repeated queries over unchanging data. "Subarray sum equals k" and "range sum query" are the standard forms.
O(n) preprocessing, then O(1) per range query — against O(n) per query when summing directly.
Underlying concept: Prefix Sum in the DSA visualizer curriculum
3 problems in this pattern
Matrices
A 2D grid is a graph whose neighbours are implicit: each cell connects to the cells beside it. Most matrix problems are BFS, DFS or DP with row and column bookkeeping, plus in-place tricks for rotation and traversal.
The input is a 2D grid and the question involves islands, regions, flood fill, spiral or diagonal traversal, path counting, or rotating and transposing in place.
O(rows × cols) for a full traversal. In-place transformations keep space at O(1) beyond the input.
3 problems in this pattern
Frequently asked questions
What are coding interview patterns?
A pattern is a reusable problem-solving technique — sliding window, two pointers, binary search on the answer — that applies across many different-looking questions. Interview problems are usually variations on a small set of these, so recognising which pattern a question wants is most of the work of solving it.
How many patterns do I actually need to know?
The 15 covered here are the ones that recur most often in technical interviews. They are not an exhaustive list of algorithmic techniques, and no fixed number guarantees coverage of any particular interview — but they are the set that gives the most return for the time invested.
Should I learn patterns or just solve more problems?
Solving problems without a framework tends to produce recall rather than transfer: you recognise questions you have seen and stall on ones you have not. Learning the pattern first gives you something to apply to an unfamiliar problem. Most people need both — the pattern to know what to try, and the practice to make it automatic.
Do I need to know data structures before starting patterns?
Yes, at least the fundamentals. Patterns are built on arrays, hash maps, stacks, trees and graphs. If those are shaky, work through the DSA visualizer curriculum first — it covers the same structures with a visualizer for each.
Are the pattern lessons free?
Each pattern includes free starter problems, marked "Free" in the lists above, which open without payment. The full library is part of a one-time purchase with lifetime access and no subscription.
