LeetCode Patterns: 14 Templates That Cover 95% of Problems
Most engineers preparing for technical interviews make the same mistake: they grind problems randomly, hoping volume leads to pattern recognition. The better approach is to learn the patterns first...

Source: DEV Community
Most engineers preparing for technical interviews make the same mistake: they grind problems randomly, hoping volume leads to pattern recognition. The better approach is to learn the patterns first, then use problems as practice for applying them. Here are 14 patterns with reusable code templates that appear across the overwhelming majority of LeetCode-style interview questions. Pattern 1: Two Pointers When to use: Arrays or strings where you need to find pairs, compare elements from both ends, or partition data. Classic problems: Two Sum (sorted), Container With Most Water, Valid Palindrome, 3Sum def two_pointers(arr): left, right = 0, len(arr) - 1 while left < right: current = arr[left] + arr[right] if current == target: return [left, right] elif current < target: left += 1 else: right -= 1 return [] Key insight: Two pointers eliminate the inner loop, reducing O(n²) brute force to O(n). Pattern 2: Sliding Window When to use: Problems involving contiguous subarrays/substrings wi