5 Essential LeetCode Patterns for Technical Interviews
Mastering these five LeetCode patterns will help you solve a wide range of coding interview problems efficiently. Each pattern comes with practice problems to reinforce your understanding.
1. Two Pointers
This pattern uses two pointers to traverse data structures, either moving in the same direction (fast/slow) or opposite directions (opposite ends).
Two Pointers (Opposite Ends)
Pseudocode Example: Two Sum II (Opposite Ends)
function twoSum(numbers, target):
left = 0
right = length(numbers) - 1
while left < right:
current_sum = numbers[left] + numbers[right]
if current_sum == target:
return [left + 1, right + 1] # 1-based index
elif current_sum < target:
left++
else:
right--
return [-1, -1] # No solution found
Practice Problems: 167. Two Sum II, 15. 3Sum, 42. Trapping Rain Water, 11. Container With Most Water, 3. Longest Substring Without Repeating Characters
2. Sliding Window
Maintains a window of elements in an array/string, which can be either fixed or variable in size.
Sliding Window Example
For the string 'abcabcbb', the sliding window expands when new unique characters are found and contracts when duplicates are encountered, keeping track of the maximum window size with all unique characters.
Pseudocode Example: Longest Substring Without Repeating Characters
function lengthOfLongestSubstring(s):
charSet = new Set()
left = 0
maxLength = 0
for right in range(len(s)):
while s[right] in charSet:
charSet.remove(s[left])
left++
charSet.add(s[right])
maxLength = max(maxLength, right - left + 1)
return maxLength
Practice Problems: 3. Longest Substring Without Repeating Characters, 76. Minimum Window Substring, 424. Longest Repeating Character Replacement, 209. Minimum Size Subarray Sum, 1004. Max Consecutive Ones III
3. Binary Search on Answer
Applies binary search to find the optimal solution in a sorted search space.
Binary Search on Answer
Pseudocode Example: Koko Eating Bananas
function minEatingSpeed(piles, h):
left = 1
right = max(piles)
result = right
while left <= right:
mid = left + (right - left) // 2
hours = 0
for pile in piles:
hours += ceil(pile / mid)
if hours <= h:
result = min(result, mid)
right = mid - 1
else:
left = mid + 1
return result
Practice Problems: 875. Koko Eating Bananas, 410. Split Array Largest Sum, 1011. Capacity To Ship Packages Within D Days, 1552. Magnetic Force Between Two Balls, 4. Median of Two Sorted Arrays
4. DFS + Backtracking
Explores all possible solutions by trying different choices and undoing them if they don't work.
DFS + Backtracking (Subsets of [1,2,3])
Pseudocode Example: Subsets
function subsets(nums):
result = []
def backtrack(start, current):
result.append(current.copy())
for i in range(start, len(nums)):
# Include the current element
current.append(nums[i])
# Move to the next element
backtrack(i + 1, current)
# Backtrack (exclude the current element)
current.pop()
backtrack(0, [])
return result
Practice Problems: 78. Subsets, 90. Subsets II, 46. Permutations, 79. Word Search, 51. N-Queens
5. Top-Down DP with Memoization
Breaks down problems into smaller subproblems and stores their solutions to avoid redundant calculations.
DP Memoization Approach
In the unique paths problem, we build a DP table where each cell (i,j) represents the number of ways to reach it from the start. Each cell's value is the sum of the cell above it and the cell to its left.
Pseudocode Example: Unique Paths
function uniquePaths(m, n):
memo = 2D array of size m x n, initialized with -1
function dp(row, col):
# Base cases
if row == m - 1 and col == n - 1:
return 1
if row >= m or col >= n:
return 0
# Check if already computed
if memo[row][col] != -1:
return memo[row][col]
# Recursive case: move right + move down
memo[row][col] = dp(row + 1, col) + dp(row, col + 1)
return memo[row][col]
return dp(0, 0)
Practice Problems: 62. Unique Paths, 1143. Longest Common Subsequence, 416. Partition Equal Subset Sum, 494. Target Sum, 1235. Maximum Profit in Job Scheduling
Pro Tip: When practicing these patterns, focus on understanding the underlying concept rather than just memorizing solutions. Try to solve each problem multiple times until you can implement it without looking at the solution. Time yourself to improve your coding speed and efficiency.