Stop memorizing algorithms! Learn the 7 most important patterns that solve 90% of coding interview problems. Simple explanations, clean code, real examples.
When to use: Sorted arrays, finding pairs, palindromes
JAVASCRIPT1// Pattern: Start from both ends, move towards center 2let left = 0; 3let right = array.length - 1; 4 5while (left < right) { 6 // Do something with array[left] and array[right] 7 if (condition) { 8 left++; 9 } else { 10 right--; 11 } 12}
JAVASCRIPT1function twoSum(nums, target) { 2 let left = 0; 3 let right = nums.length - 1; 4 5 while (left < right) { 6 const sum = nums[left] + nums[right]; 7 8 if (sum === target) return [left, right]; 9 if (sum < target) left++; 10 else right--; 11 } 12 return []; 13} 14 15// Try it: twoSum([2, 7, 11, 15], 9) ā [0, 1]
JAVASCRIPT1function isPalindrome(s) { 2 s = s.toLowerCase().replace(/[^a-z0-9]/g, ''); 3 let left = 0, right = s.length - 1; 4 5 while (left < right) { 6 if (s[left] !== s[right]) return false; 7 left++; 8 right--; 9 } 10 return true; 11} 12 13// Try it: isPalindrome("A man, a plan, a canal: Panama") ā true
When to use: Subarrays, substrings, "find max/min in window"
JAVASCRIPT1let left = 0; 2for (let right = 0; right < array.length; right++) { 3 // Expand window by including array[right] 4 5 while (/* window is invalid */) { 6 // Shrink window by removing array[left] 7 left++; 8 } 9 10 // Update result with current window 11}
JAVASCRIPT1function longestUniqueSubstring(s) { 2 const seen = new Set(); 3 let left = 0; 4 let maxLength = 0; 5 6 for (let right = 0; right < s.length; right++) { 7 // Shrink window until no duplicates 8 while (seen.has(s[right])) { 9 seen.delete(s[left]); 10 left++; 11 } 12 13 seen.add(s[right]); 14 maxLength = Math.max(maxLength, right - left + 1); 15 } 16 17 return maxLength; 18} 19 20// Try it: longestUniqueSubstring("abcabcbb") ā 3 ("abc")
JAVASCRIPT1function maxSumSubarray(nums, k) { 2 let windowSum = nums.slice(0, k).reduce((a, b) => a + b); 3 let maxSum = windowSum; 4 5 for (let i = k; i < nums.length; i++) { 6 windowSum = windowSum - nums[i - k] + nums[i]; 7 maxSum = Math.max(maxSum, windowSum); 8 } 9 10 return maxSum; 11} 12 13// Try it: maxSumSubarray([1, 4, 2, 10, 23, 3], 3) ā 39
When to use: Linked lists, cycle detection, finding middle element
JAVASCRIPT1let slow = head; 2let fast = head; 3 4while (fast && fast.next) { 5 slow = slow.next; // Move 1 step 6 fast = fast.next.next; // Move 2 steps 7 8 if (slow === fast) { // They meet = cycle found! 9 return true; 10 } 11}
JAVASCRIPT1function hasCycle(head) { 2 let slow = head; 3 let fast = head; 4 5 while (fast && fast.next) { 6 slow = slow.next; 7 fast = fast.next.next; 8 9 if (slow === fast) return true; 10 } 11 12 return false; 13}
JAVASCRIPT1function findMiddle(head) { 2 let slow = head; 3 let fast = head; 4 5 while (fast && fast.next) { 6 slow = slow.next; 7 fast = fast.next.next; 8 } 9 10 return slow; // This is the middle! 11}
When to use: Meeting rooms, calendars, overlapping ranges
JAVASCRIPT1// 1. Sort intervals by start time 2intervals.sort((a, b) => a[0] - b[0]); 3 4// 2. Merge overlapping intervals 5const merged = [intervals[0]]; 6 7for (let i = 1; i < intervals.length; i++) { 8 const current = intervals[i]; 9 const last = merged[merged.length - 1]; 10 11 if (current[0] <= last[1]) { 12 // Overlap! Merge them 13 last[1] = Math.max(last[1], current[1]); 14 } else { 15 // No overlap, add new interval 16 merged.push(current); 17 } 18}
JAVASCRIPT1function mergeIntervals(intervals) { 2 if (intervals.length <= 1) return intervals; 3 4 intervals.sort((a, b) => a[0] - b[0]); 5 const merged = [intervals[0]]; 6 7 for (let i = 1; i < intervals.length; i++) { 8 const current = intervals[i]; 9 const last = merged[merged.length - 1]; 10 11 if (current[0] <= last[1]) { 12 last[1] = Math.max(last[1], current[1]); 13 } else { 14 merged.push(current); 15 } 16 } 17 18 return merged; 19} 20 21// Try it: mergeIntervals([[1,3],[2,6],[8,10]]) ā [[1,6],[8,10]]
When to use: Any tree problem, path finding, level-order processing
JAVASCRIPT1function dfs(node) { 2 if (!node) return; 3 4 // Process current node 5 console.log(node.val); 6 7 // Visit children 8 dfs(node.left); 9 dfs(node.right); 10}
JAVASCRIPT1function bfs(root) { 2 const queue = [root]; 3 4 while (queue.length > 0) { 5 const node = queue.shift(); 6 console.log(node.val); 7 8 if (node.left) queue.push(node.left); 9 if (node.right) queue.push(node.right); 10 } 11}
JAVASCRIPT1function maxDepth(root) { 2 if (!root) return 0; 3 4 const leftDepth = maxDepth(root.left); 5 const rightDepth = maxDepth(root.right); 6 7 return Math.max(leftDepth, rightDepth) + 1; 8}
When to use: Generate all combinations, permutations, solve puzzles
JAVASCRIPT1function backtrack(currentSolution) { 2 if (/* solution is complete */) { 3 result.push([...currentSolution]); 4 return; 5 } 6 7 for (let choice of possibleChoices) { 8 // Make choice 9 currentSolution.push(choice); 10 11 // Explore 12 backtrack(currentSolution); 13 14 // Undo choice (backtrack!) 15 currentSolution.pop(); 16 } 17}
JAVASCRIPT1function permute(nums) { 2 const result = []; 3 4 function backtrack(current) { 5 if (current.length === nums.length) { 6 result.push([...current]); 7 return; 8 } 9 10 for (let num of nums) { 11 if (current.includes(num)) continue; 12 13 current.push(num); // Make choice 14 backtrack(current); // Explore 15 current.pop(); // Undo choice 16 } 17 } 18 19 backtrack([]); 20 return result; 21} 22 23// Try it: permute([1,2,3]) ā [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
When to use: Optimization problems, "find maximum/minimum", counting problems
JAVASCRIPT1// Step 1: Define what dp[i] represents 2// Step 2: Find the recurrence relation 3// Step 3: Handle base cases 4// Step 4: Fill the dp table 5 6const dp = new Array(n + 1).fill(0); 7dp[0] = baseCase; 8 9for (let i = 1; i <= n; i++) { 10 dp[i] = /* some function of previous dp values */; 11} 12 13return dp[n];
JAVASCRIPT1function fibonacci(n) { 2 if (n <= 1) return n; 3 4 const dp = [0, 1]; 5 6 for (let i = 2; i <= n; i++) { 7 dp[i] = dp[i - 1] + dp[i - 2]; 8 } 9 10 return dp[n]; 11} 12 13// Even better - O(1) space: 14function fibonacciOptimal(n) { 15 if (n <= 1) return n; 16 17 let prev = 0, curr = 1; 18 19 for (let i = 2; i <= n; i++) { 20 [prev, curr] = [curr, prev + curr]; 21 } 22 23 return curr; 24}
JAVASCRIPT1function climbStairs(n) { 2 if (n <= 2) return n; 3 4 let oneStep = 1, twoSteps = 2; 5 6 for (let i = 3; i <= n; i++) { 7 [oneStep, twoSteps] = [twoSteps, oneStep + twoSteps]; 8 } 9 10 return twoSteps; 11} 12 13// Try it: climbStairs(5) ā 8 ways
Test your pattern recognition skills:
[3, 2, 4], target = 6
[1, 2, 3, 4, 5], k = 3
1ā2ā3ā4ā5
[[0,30],[5,10],[15,20]]
n = 3
"babad"
[0,1,0,2,1,0,1,3,2,1,2,1]
ā Do This:
ā Avoid This:
Happy coding! š Remember: Every expert was once a beginner. You've got this!
Like this guide if it helped you crack your next coding interview! ā
Updated