Promotion title
Promotion description
Button Text

LeetCode Patterns: 10 Core Algorithms for Coding Interviews

The 10 LeetCode patterns that cover the majority of FAANG coding interview problems, with recognition signals and canonical examples for each.
Tim Liu
Written by
Tim Liu
Haoqian Li
Edited by
Haoqian Li
Jay Ma
Reviewed by
Jay Ma
Updated on
Aug 26, 2026
Read time
12 min read
LeetCode Patterns: 10 Core Algorithms for Coding Interviews

Why LeetCode Patterns Matter More Than Individual Solutions

LeetCode patterns are recurring algorithmic structures that underpin thousands of coding interview problems. Learning the pattern is more valuable than memorizing individual solutions because a pattern-based approach lets you recognize and solve problems you have never seen before, not just repeat problems you have memorized. Mastering these fundamentals is what separates candidates who clear the coding interview from those who recognize the problem type too late to structure an efficient solution.

LeetCode pattern frequency in FAANG interviews: Dynamic Programming 25%, Tree BFS/DFS 18%, Two Pointers 14%, Sliding Window 12%, Graph BFS/DFS 10%, Binary Search 8%, Backtracking 6%, Heap 4%, Merge Intervals 2%, Cyclic Sort 1%
Dynamic Programming and tree traversal together account for 43% of FAANG coding interview problems, making them the highest-return patterns to master before a coding screen.

After tagging problems by core technique, researchers have found that roughly 8 to 10 patterns cover the majority of what appears in FAANG and top-tier company coding interviews. This guide covers the 10 most important patterns, when to reach for each one, and how to recognize which pattern applies to a new problem.

Quick Answer

  • LeetCode patterns are the recurring algorithmic structures that underpin most coding interview problems at top companies.
  • The 10 core patterns are: Sliding Window, Two Pointers, Fast and Slow Pointers, BFS, DFS, Binary Search, Dynamic Programming, Merge Intervals, Topological Sort, and Heap.
  • Recognizing which pattern to apply requires reading three signals: the input shape, the input size, and the optimization target stated in the problem.

How to Recognize Which Pattern to Use

When you open any coding problem, extract three signals before thinking about a solution. These signals narrow the candidate patterns from ten down to one or two.

Signal 1: Input shape. Is the input an array or string? Is it sorted? Is it a linked list? A tree? A graph or grid? The data structure constrains which patterns are applicable. A sorted array points toward Two Pointers or Binary Search. A tree points toward DFS or BFS. A grid points toward BFS or DFS depending on whether shortest path is required.

Signal 2: Input size. An input size of 10 to the power of 5 rules out O(n squared) approaches and points toward linear or log-linear patterns like Sliding Window, Two Pointers, or Heap. An input size of 20 or fewer suggests backtracking or bitmask dynamic programming, because exponential time is affordable at that scale.

Signal 3: Optimization target. Are you minimizing, maximizing, or counting? "Minimum number of steps" on an unweighted graph is BFS. "Maximum sum of a subarray of length k" is Sliding Window. "Number of ways to reach a target" is Dynamic Programming. "Find all paths" is DFS with backtracking.

Pattern 1: Sliding Window

The Sliding Window pattern optimizes problems involving contiguous subarrays or substrings. Instead of recalculating results for every possible window from scratch, you maintain a window and slide it across the input, adjusting the result by adding the new element entering the window and removing the element leaving it.

Without Sliding Window, the brute-force approach requires two nested loops with O(n squared) time. The Sliding Window version is O(n) time because each element enters and exits the window at most once.

When to use it: The problem mentions "contiguous subarray," "consecutive elements," or "substring." Keywords like "maximum sum subarray of length k," "longest substring without repeating characters," or "minimum window substring" all point to Sliding Window.

Fixed vs. variable window: Fixed-size windows are simpler. Variable-size windows require tracking a validity condition and expanding or shrinking accordingly. For a variable window, move the right pointer forward until the condition becomes invalid, then move the left pointer until the condition is valid again.

Canonical problems: Maximum Sum Subarray of Size K, Longest Substring Without Repeating Characters (LeetCode 3), Minimum Window Substring (LeetCode 76).

Pattern 2: Two Pointers

The Two Pointers pattern uses two indices to traverse a data structure, typically from opposite ends or with a fixed separation. It reduces O(n squared) brute-force approaches that use nested loops to O(n) linear time by allowing both pointers to advance based on comparison logic rather than exhaustive search.

When to use it: The problem involves a sorted array or string, and you are looking for pairs, triplets, or a partition condition. Two Sum II (sorted input), 3Sum, Container With Most Water, and Valid Palindrome are archetypal Two Pointers problems.

Convergence pattern: Place one pointer at the start and one at the end. Move them toward each other based on a comparison condition. This handles pair sum, palindrome check, and container maximum area problems.

Slow-fast pattern: One pointer advances one step at a time while the other advances two or three steps. This handles linked list cycle detection, finding the middle node, and similar problems where relative position matters more than absolute position.

Canonical problems: Two Sum II (LeetCode 167), 3Sum (LeetCode 15), Remove Duplicates from Sorted Array (LeetCode 26).

Pattern 3: Fast and Slow Pointers

Fast and Slow Pointers, also called Floyd's Cycle Detection, solves problems in linked lists where you need to detect cycles, find the middle node, or find the entry point of a cycle. The fast pointer moves two steps while the slow pointer moves one step. If a cycle exists, they will meet.

When to use it: The problem involves a linked list and asks whether a cycle exists, where the cycle begins, or what the middle element is. Any problem with "linked list" and "cycle" points to this pattern.

Canonical problems: Linked List Cycle (LeetCode 141), Find the Duplicate Number (LeetCode 287), Middle of the Linked List (LeetCode 876).

Pattern 4: BFS (Breadth-First Search)

BFS explores a graph or tree level by level using a queue. It finds the shortest path in an unweighted graph and processes nodes in the order they are encountered by distance from the source. BFS guarantees the shortest path because it explores all neighbors at the current distance before moving to the next distance.

When to use it: "Minimum number of steps" on an unweighted graph is a BFS tell. Problems involving level-order processing of trees, shortest path on a grid, or finding connected components at minimum distance all point to BFS.

Implementation: Initialize a queue with the starting node and a visited set. While the queue is not empty, dequeue the current node, process it, and enqueue its unvisited neighbors, marking each as visited when enqueued. For grids, neighbors are up, down, left, and right cells.

Canonical problems: Binary Tree Level Order Traversal (LeetCode 102), Number of Islands (LeetCode 200), Shortest Path in Binary Matrix (LeetCode 1091).

Pattern 5: DFS (Depth-First Search)

DFS explores as far as possible along each branch before backtracking. It is implemented recursively or with an explicit stack. DFS is used for problems requiring complete exploration, path finding, connectivity, and tree traversal in pre-order, in-order, or post-order.

When to use it: "Find all paths" or "does a path exist" is DFS. Tree problems that require processing parent before children (pre-order) or children before parent (post-order) are DFS. Permutations and combinations are DFS with backtracking.

Canonical problems: Path Sum (LeetCode 112), Clone Graph (LeetCode 133), Permutations (LeetCode 46), Word Search (LeetCode 79).

Pattern 6: Binary Search

Binary Search reduces the search space by half at each step by comparing the target against the middle element. It requires a sorted input or a monotonic condition. Binary Search is O(log n) time.

When to use it: The problem has a sorted array and asks for a target element, the first element meeting a condition, or the minimum or maximum value satisfying a constraint. Problems that ask "find the minimum maximum" or "minimize the maximum value" in a sorted or monotonic space are Binary Search on the answer.

Canonical problems: Binary Search (LeetCode 704), Search in Rotated Sorted Array (LeetCode 33), Find Minimum in Rotated Sorted Array (LeetCode 153), Capacity to Ship Packages (LeetCode 1011).

Pattern 7: Dynamic Programming

Dynamic Programming solves optimization and counting problems where subproblems overlap. Instead of recomputing the same subproblem multiple times, you cache results and build the final answer from cached subresults. The hard part is defining the state and the recurrence relation. Once you have both, the implementation is usually straightforward.

When to use it: The problem asks for the maximum, minimum, or count of something, and smaller versions of the same problem are embedded in the original. "Longest increasing subsequence," "number of ways to decode a string," and "minimum coins to make change" are all DP problems.

Two approaches: top-down with memoization (recursive with a cache) and bottom-up with tabulation (iterative with a table). Bottom-up is typically more memory-efficient. Top-down is often easier to write correctly from the recurrence relation.

Canonical problems: Climbing Stairs (LeetCode 70), Coin Change (LeetCode 322), Longest Common Subsequence (LeetCode 1143), House Robber (LeetCode 198).

Pattern 8: Merge Intervals

The Merge Intervals pattern solves problems involving overlapping time windows, ranges, or intervals. Sort the intervals by start time, then iterate through and merge any two intervals that overlap by comparing the end time of the current interval against the start time of the next.

When to use it: The problem gives you a list of intervals and asks you to merge overlapping ones, find conflicts, or insert a new interval into an existing sorted list. Meeting room scheduling, calendar conflict detection, and job scheduling problems all use this pattern.

Canonical problems: Merge Intervals (LeetCode 56), Insert Interval (LeetCode 57), Meeting Rooms II (LeetCode 253).

Pattern 9: Topological Sort

Topological Sort orders the nodes in a directed acyclic graph so that every directed edge goes from an earlier node to a later node. It is used for problems with dependency ordering: completing tasks in order, course prerequisite scheduling, and build system dependency resolution.

When to use it: The problem involves directed dependencies, ordering with constraints, or cycle detection in a directed graph. "Can you finish all courses given prerequisites?" is a cycle detection problem solved with topological sort.

Canonical problems: Course Schedule (LeetCode 207), Course Schedule II (LeetCode 210), Alien Dictionary (LeetCode 269).

Pattern 10: Heap (Priority Queue)

A Heap maintains a sorted priority order for elements as you insert and remove them, giving O(log n) insertion and O(1) access to the minimum or maximum element. It is used when you need the k largest or smallest elements, or when you need to process elements in priority order without sorting the entire input upfront.

When to use it: "Find the k largest elements," "k-th smallest element," or "merge k sorted lists" all point to a Heap. Any problem where you repeatedly need the current minimum or maximum of a dynamic set is a Heap problem.

Canonical problems: Kth Largest Element in a Stream (LeetCode 703), Top K Frequent Elements (LeetCode 347), Merge K Sorted Lists (LeetCode 23), Find Median from Data Stream (LeetCode 295).

Recommended Study Order

Start with the patterns that give the highest return per hour of study: Two Pointers, Sliding Window, and Binary Search. These three patterns together cover a significant fraction of easy and medium problems. Then add BFS, DFS, and Backtracking. Give Dynamic Programming at least one dedicated week because DP builds on itself and the pattern recognition requires seeing many problem types. Finish with Heap, Topological Sort, Union Find, Trie, Monotonic Stack, and Bit Manipulation.

For coding interview preparation at FAANG and top-tier companies in 2025 and 2026, the AI interview help for LeetCode guide covers how to use AI tools to accelerate pattern recognition and practice effectively. Candidates preparing for technical rounds at Google, Meta, and Amazon have shared which patterns appear most frequently in the Final Round AI community discussion on Meta coding interview difficulty versus LeetCode. Browse more technical interview guides in the technical interviews category.

Practice the system design cheat sheet alongside LeetCode patterns, since FAANG interviews test both in separate rounds and the strongest candidates are prepared for both.

Author's Comment

"The candidates who pass coding screens consistently are the ones who have internalized the signal-reading step. Before they write a single line of code, they have identified the input shape, the input size, and the optimization target, and those three signals have already narrowed the candidate patterns to one or two. That takes about 30 seconds. Candidates who skip that step and jump to a solution are the ones who code for 40 minutes and then realize they used the wrong data structure."

Tim Liu, Technical Interview Specialist at Final Round AI

Candidates who use AI interview coaching to practice before the real interview typically feel more prepared and confident going in.

Related Interview Guides

Frequently Asked Questions

What are LeetCode patterns?

LeetCode patterns are recurring algorithmic structures that underpin the majority of coding interview problems. Learning a pattern lets you solve new problems that use the same structure without memorizing each problem individually. The 10 core patterns cover Sliding Window, Two Pointers, BFS, DFS, Binary Search, Dynamic Programming, Fast and Slow Pointers, Merge Intervals, Topological Sort, and Heap.

How many LeetCode patterns are there?

Estimates vary from 8 to 42 depending on how granular the classification is. For practical FAANG interview preparation, 10 core patterns cover the vast majority of what appears. Extended lists add patterns like Union Find, Trie, Monotonic Stack, Bitmask DP, and Segment Tree for companies with harder technical bars.

How do I know which LeetCode pattern to use?

Read three signals from the problem: the input shape (array, linked list, tree, graph), the input size (rules out or in specific time complexities), and the optimization target (minimize, maximize, count, find all). These three signals narrow the applicable patterns from ten to one or two in most cases.

Is dynamic programming harder than other LeetCode patterns?

Yes, for most candidates. DP requires defining the state and the recurrence relation before writing any code, and that step is not intuitive until you have seen many DP problems. One dedicated week of DP practice, working through the canonical problems in roughly this order: Climbing Stairs, House Robber, Coin Change, Longest Common Subsequence, gives you enough exposure for the pattern to start feeling recognizable.

How many LeetCode problems do I need to solve to pass FAANG interviews?

Related reading: Big O notation in Java.

Quality matters more than quantity. 50 to 100 problems solved with genuine pattern understanding beats 300 problems solved through memorization. For each problem, write the solution, identify which pattern it uses, write test cases, and explain your reasoning out loud. That full cycle per problem builds the recognition and articulation skills that interviews actually test.

Practice LeetCode Patterns in Real Interview Format

Knowing the patterns is one step. Articulating your reasoning under pressure while an interviewer watches is another. Interview CoPilot™ includes real-time support during live technical interviews. When the interviewer presents a coding problem, Interview CoPilot™ can surface a structured approach suggestion while you work through it on CoderPad. Download the Interview CoPilot™ desktop app and practice your pattern-based thinking in a real interview environment before your next coding screen.

{"@context":"https://schema.org","@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What are LeetCode patterns?","acceptedAnswer":{"@type":"Answer","text":"LeetCode patterns are recurring algorithmic structures that underpin the majority of coding interview problems. Learning a pattern lets you solve new problems that use the same structure without memorizing each problem individually. The 10 core patterns cover Sliding Window, Two Pointers, BFS, DFS, Binary Search, Dynamic Programming, Fast and Slow Pointers, Merge Intervals, Topological Sort, and Heap."}},{"@type":"Question","name":"How many LeetCode patterns are there?","acceptedAnswer":{"@type":"Answer","text":"Estimates vary from 8 to 42 depending on granularity. For practical FAANG interview preparation, 10 core patterns cover the vast majority. Extended lists add Union Find, Trie, Monotonic Stack, and Bitmask DP for harder technical bars."}},{"@type":"Question","name":"How do I know which LeetCode pattern to use?","acceptedAnswer":{"@type":"Answer","text":"Read three signals: the input shape (array, linked list, tree, graph), the input size (rules out specific time complexities), and the optimization target (minimize, maximize, count, find all). These narrow the applicable patterns from ten to one or two in most cases."}},{"@type":"Question","name":"Is dynamic programming harder than other LeetCode patterns?","acceptedAnswer":{"@type":"Answer","text":"Yes, for most candidates. DP requires defining the state and the recurrence relation before writing any code. One dedicated week working through canonical problems in order — Climbing Stairs, House Robber, Coin Change, Longest Common Subsequence — builds the recognition needed."}},{"@type":"Question","name":"How many LeetCode problems do I need to solve to pass FAANG interviews?","acceptedAnswer":{"@type":"Answer","text":"Quality matters more than quantity. 50 to 100 problems solved with genuine pattern understanding beats 300 solved through memorization. For each problem: write the solution, identify the pattern, write test cases, and explain your reasoning out loud."}}]}

Your competition is already using AI in their interviews

Real-time answer suggestions, live in your Interview. 100,000+ candidates use it at Google, Amazon, Meta and more.

Table of Contents

Never go blank in your next interview

Interview Copilot listens live and tells you what to say next, so every answer lands, even the ones you didn't prep for.

Related articles