# 8 Common LeetCode Patterns To Pass Coding Interviews

LeetCode patterns are common ways of approaching an optimal solution to a coding question on the LeetCode platform. The patterns vary in complexity to understand and apply and are important to know in order to solve a greater breadth of LeetCode questions efficiently and quickly. These patterns also apply to the coding interview questions that popular large tech companies like Google, Amazon, Apple, and Microsoft ask their software engineering job candidates.

In this article, we discuss what LeetCode patterns are in more detail and then describe the 8 most common LeetCode patterns that are necessary to learn to pass coding interviews. At the end of the article, we’ll discuss how you can get better at recognizing LeetCode patterns and solving LeetCode problems.

## What Are LeetCode Patterns?

LeetCode patterns are **recurring approaches, strategies, or templates that appear in optimal solutions to solve data structure and algorithm problems on the LeetCode website.** These patterns range in their difficulty in learning, their difficulty in recognizing a question, and their difficulty in applying to problems.

LeetCode patterns are highly prevalent in the vast majority of coding interview questions for software engineers that companies like Google, Amazon, and Meta conduct. So, if you want to pass coding interviews, that’s one reason it’s important to recognize the most common LeetCode patterns.

### Why Are LeetCode Patterns Important To Recognize?

LeetCode patterns are important to recognize **for reasons such as improving your chances of passing a coding interview, improving your ability to solve LeetCode problems faster, and improving your ability to solve more LeetCode problems.**

## What Are The 8 Most Common LeetCode Patterns Used In Interviews?

The 8 most common LeetCode patterns that are used in interviews are **the two pointer, fast and slow pointer, binary search, sliding window, recursion, depth-first search, breadth-first search, and topological sort patterns**. Learning these patterns will greatly improve your chances of passing coding interviews because it’s rare for an interview problem to not cover at least one of these topics.

For each of these patterns in the sections below, we describe what each of these essential LeetCode patterns are and the types of LeetCode problems they solve. We also provide 5 examples of LeetCode questions that require each specific pattern in an optimal solution.

### 1. Two Pointer

The two-pointer LeetCode pattern is a pattern in which you maintain track of two positions of data within a data structure using two variables. This is a technique that is commonly used to solve a variety of problems, both easy and hard, that are asked throughout the software engineer interview process.

For string and array problems that require the two-point technique, the technique is done by using two integer variables that track relevant indices within the string and array. For linked list and graph problems that require the two-pointer technique, the technique is done by using two pointers to references of “nodes” that track relevant nodes within a data structure.

The table below depicts LeetCode questions that can be solved using the two-pointer LeetCode pattern. The columns represent the LeetCode ID of the question, the name of the LeetCode question, and a short description of the LeetCode problem.

LeetCode ID | LeetCode Name | Description Of LeetCode Problem |

1 | Two Sum | Find two numbers in an array that add up to a specific target. |

15 | 3Sum | Find all unique triplets in an array that sum to zero. |

26 | Remove Duplicates from Sorted Array | Remove duplicates from a sorted array in-place. |

88 | Merge Sorted Array | Merge two sorted arrays into one sorted array. |

167 | Two Sum II – Input array is sorted | Find two numbers in a sorted array that add up to a specific target. |

### 2. Fast And Slow Pointer

The fast and slow pointer LeetCode pattern is a pattern for solving LeetCode problems in which you utilize two pointers moving through the array or linked list at different speeds. This is a LeetCode technique that is commonly used to solve problems related to linked lists and arrays, often focusing on medium to hard difficulty levels and involving cycles, palindromes, or other specific conditions.

For array and string problems that require the fast and slow pointer technique, the technique is implemented by using two integer variables that track relevant indices at different speeds, usually one moving twice as fast as the other. For linked list and graph problems that require the Fast and Slow Pointer technique, the technique is performed by using two pointers to references of “nodes” that track relevant nodes within the data structure at different speeds, generally one advancing faster than the other.

The table below depicts LeetCode questions that can be solved using the fast and slow pointer LeetCode pattern. The columns represent the LeetCode ID of the question, the name of the LeetCode question, and a short description of the LeetCode problem.

LeetCode ID | LeetCode Name | Description Of LeetCode Problem |

141 | Linked List Cycle | Determine if a linked list has a cycle. |

142 | Linked List Cycle II | Find the starting node of the cycle in a linked list. |

202 | Happy Number | Determine if a number is a “happy number.” |

876 | Middle of the Linked List | Find the middle node of a linked list. |

160 | Intersection of Two Linked Lists | Find the node where two linked lists intersect. |

### 3. Binary Search

The binary search LeetCode pattern is a pattern for solving LeetCode problems in which you divide and conquer a sorted array or list by recursively or iteratively eliminating half of the remaining elements. This is a LeetCode technique that is commonly used to solve problems requiring efficient searching or finding a specific condition, often under time constraints, and it is usually tagged as easy to hard based on problem complexity.

For array and list problems that require the binary search technique, the technique is implemented by using an integer variable to track the middle index and other variables to maintain the boundaries of the search range. For string problems that require binary search, the technique is executed similarly, but focuses on character comparisons instead of numerical values.

The table below depicts LeetCode questions that can be solved using the binary search LeetCode pattern. The columns represent the LeetCode ID of the question, the name of the LeetCode question, and a short description of the LeetCode problem.

LeetCode ID | LeetCode Name | Description Of LeetCode Problem |

035 | Search Insert Position | Find the insert position of a target in a sorted array. |

069 | Sqrt(x) | Compute the square root of a number without using any built-in functions. |

278 | First Bad Version | Find the first “bad version” in a list of versions. |

704 | Binary Search | Implement basic binary search on a sorted array. |

153 | Find Minimum in Rotated Sorted Array | Find the minimum element in a rotated sorted array. |

### 4. Sliding Window

The sliding window LeetCode pattern is a pattern for solving LeetCode problems in which you maintain a subarray or substring window of variables and slide it through the given array or string to solve optimization problems. This is a LeetCode technique that is commonly used to solve problems related to arrays and strings, especially focusing on minimum or maximum conditions, often categorized under medium to hard difficulties.

For array problems that require the sliding window technique, the technique is performed by using integer variables that denote the start and end of the window, as well as additional variables for maintaining any required summaries like sums or counts. For string problems, the Sliding Window technique functions in a similar way but focuses on substrings or character occurrences within the string.

The table below depicts LeetCode questions that can be solved using the sliding window LeetCode pattern. The columns represent the LeetCode ID of the question, the name of the LeetCode question, and a short description of the LeetCode problem.

LeetCode ID | LeetCode Name | Description Of LeetCode Problem |

003 | Longest Substring Without Repeating Characters | Find the length of the longest substring without repeating characters. |

209 | Minimum Size Subarray Sum | Find the minimal length of a contiguous subarray that sums to a specific target. |

438 | Find All Anagrams in a String | Find all the start indices of anagrams of a pattern in a string. |

567 | Permutation in String | Check if the first string’s permutations include the second string. |

763 | Partition Labels | Partition a string into as many parts as possible so that each letter appears in at most one part. |

### 5. Recursion

The recursion LeetCode pattern is a pattern for solving LeetCode problems in which you break down a problem into smaller, more manageable problems and solve them by calling the function itself. This is a LeetCode technique that is commonly used to solve problems related to trees, linked lists, and backtracking algorithms, ranging from easy to hard complexities.

For tree problems that require the recursion technique, the technique is performed by breaking down the problem into smaller tree-related problems that are solved recursively. For linked list problems, Recursion is used in a similar manner, often simplifying the problem into base cases and recursive cases that deal with a node and its subsequent elements.

The table below depicts LeetCode questions that can be solved using the recursion LeetCode pattern. The columns represent the LeetCode ID of the question, the name of the LeetCode question, and a short description of the LeetCode problem.

LeetCode ID | LeetCode Name | Description Of LeetCode Problem |

021 | Merge Two Sorted Lists | Merge two sorted linked lists into one. |

104 | Maximum Depth of Binary Tree | Find the maximum depth of a binary tree. |

206 | Reverse Linked List | Reverse a linked list. |

509 | Fibonacci Number | Calculate the Fibonacci number at a given index. |

700 | Search in a Binary Search Tree | Search for a value in a Binary Search Tree. |

### 6. Depth-first Search (DFS)

The depth-first Search (DFS) LeetCode pattern is a pattern for solving LeetCode problems in which you explore as far as possible along each branch before backtracking, often using a stack or recursion. This is a LeetCode technique that is commonly used to solve problems related to graphs and trees, particularly for traversals, path-finding, and connected components, usually in the medium to hard difficulty range.

For graph problems that require the depth-first Search (DFS) technique, the technique is carried out by using a stack data structure or recursive calls to explore each vertex and its adjacent vertices deeply before backtracking. For tree problems, DFS operates in a similar fashion, exploring as far down a branch as possible before considering other branches.

The table below depicts LeetCode questions that can be solved using the depth-first Search (DFS) LeetCode pattern. The columns represent the LeetCode ID of the question, the name of the LeetCode question, and a short description of the LeetCode problem.

LeetCode ID | LeetCode Name | Description Of LeetCode Problem |

046 | Permutations | Generate all possible permutations of an array. |

078 | Subsets | Generate all possible subsets of an array. |

200 | Number of Islands | Count the number of isolated “islands” in a 2D grid. |

547 | Number of Provinces | Find the number of provinces in a connectivity matrix. |

695 | Max Area of Island | Find the maximum area of an “island” in a 2D grid. |

### 7. Breadth-first Search (BFS)

The breadth-first search (BFS) LeetCode pattern is a pattern for solving LeetCode problems in which you explore all neighbor nodes at the present depth before moving on to nodes at the next depth level, usually implemented using a queue. This is a LeetCode technique that is commonly used to solve problems related to graphs and trees, especially for level-order traversal and shortest path problems, and is usually in the easy to medium difficulty range.

For graph problems that require the breadth-first search (BFS) technique, the technique is conducted by using a queue data structure to explore each vertex and its adjacent vertices level by level. For tree problems that use BFS, a similar approach is taken, where nodes at the same depth are processed before nodes at the next depth level.

The table below depicts LeetCode questions that can be solved using the breadth-first search (BFS) LeetCode pattern. The columns represent the LeetCode ID of the question, the name of the LeetCode question, and a short description of the LeetCode problem.

LeetCode ID | LeetCode Name | Description Of LeetCode Problem |

102 | Binary Tree Level Order Traversal | Return the level order traversal of a binary tree. |

127 | Word Ladder | Find the shortest transformation sequence from one word to another. |

279 | Perfect Squares | Find the least number of perfect square numbers that sum up to a given number. |

429 | N-ary Tree Level Order Traversal | Return the level order traversal of an N-ary tree. |

733 | Flood Fill | Perform a flood fill operation on an image. |

### 8. Topological Sort

The topological sort LeetCode pattern is a pattern for solving LeetCode problems in which you linearly order vertices in a directed graph such that for every directed edge (u, v), vertex u comes before v in the ordering. This is a LeetCode technique that is commonly used to solve problems related to graphs, particularly for finding dependencies or scheduling tasks, and they are generally of medium to hard difficulty.

For graph problems that require the topological sort technique, the technique is performed by using a stack to keep track of vertices in a way that all incoming edges to a vertex are processed before the vertex itself. In cases that involve dependency resolution or scheduling tasks, Topological Sort uses similar methods, often combined with a counter for incoming edges to each vertex.

The table below depicts LeetCode questions that can be solved using the topological sort LeetCode pattern. The columns represent the LeetCode ID of the question, the name of the LeetCode question, and a short description of the LeetCode problem.

LeetCode ID | LeetCode Name | Description Of LeetCode Problem |

207 | Course Schedule | Determine if it is possible to finish all courses given prerequisites. |

210 | Course Schedule II | Find a possible ordering to finish all courses given prerequisites. |

269 | Alien Dictionary | Find the order of characters in an alien language. |

310 | Minimum Height Trees | Find all the minimum height trees for a given undirected graph. |

444 | Sequence Reconstruction | Check if a sequence can be reconstructed by a list of sequences. |

## How Do You Improve Your LeetCode Skills To Recognize Patterns Easily?

You improve your LeetCode skills to recognize LeetCode patterns more easily by learning about the most important data structures and algorithms that are necessary to pass coding interviews. This is because LeetCode patterns utilize both common data structures and common algorithms in their approaches, making patterns difficult to understand and apply if you don’t understand data structures and algorithms.

### What Are The 9 Most Important Data Structures For Solving LeetCode Questions?

The 9 most important data structures for solving LeetCode questions and coding interview questions are strings, arrays, hash tables, stacks, queues, linked lists, trees, heaps, and graphs. At least one of these 9 important data structures for coding interviews is included within each LeetCode pattern.

### What Are The Most Important Types Of Algorithms For Solving LeetCode Questions?

The 6 most important types of algorithms for solving LeetCode questions and coding interview questions are sorting algorithms, searching algorithms, recursive algorithms, tree traversal algorithms, graph algorithms, and sliding window algorithms. At least one of these 6 important types of algorithms for coding interviews is included within each LeetCode pattern.