Technical Interview Patterns
Quick Reference Guide for Coding Interviews
Two Pointers
When to Use:
- Sorted arrays/linked lists
- Finding pairs with specific sum
- Removing duplicates
- Palindrome checking
Code Pattern:
let left = 0, right = arr.length - 1;
while (left < right) {
// Process based on condition
if (condition) {
left++;
} else {
right--;
}
}
Complexity:
Time: O(n)
Space: O(1)
Example Problems:
- Two Sum (sorted array)
- Container With Most Water
- Valid Palindrome
Sliding Window
When to Use:
- Contiguous subarrays/substrings
- Finding longest/shortest with condition
- Fixed or variable window size
Code Pattern:
let left = 0;
for (let right = 0; right < arr.length; right++) {
// Expand window
window.add(arr[right]);
// Contract if needed
while (!isValid) {
window.delete(arr[left]);
left++;
}
}
Complexity:
Time: O(n)
Space: O(k)
Example Problems:
- Longest Substring Without Repeating
- Maximum Sum Subarray of Size K
- Minimum Window Substring
Fast & Slow Pointers
When to Use:
- Cycle detection in linked list
- Finding middle element
- Finding cycle start
- Happy number problem
Code Pattern:
let slow = head, fast = head;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
if (slow === fast) {
// Cycle detected
break;
}
}
Complexity:
Time: O(n)
Space: O(1)
Example Problems:
- Linked List Cycle
- Find Middle of Linked List
- Palindrome Linked List
Tree Traversal (DFS/BFS)
When to Use:
- Tree/graph problems
- Level-order traversal (BFS)
- Path finding (DFS)
- Tree properties validation
Code Pattern:
// DFS Recursive
function dfs(node) {
if (!node) return;
process(node);
dfs(node.left);
dfs(node.right);
}
// BFS Iterative
const queue = [root];
while (queue.length > 0) {
const node = queue.shift();
process(node);
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
Complexity:
Time: O(n)
Space: O(h) or O(n)
Example Problems:
- Binary Tree Level Order Traversal
- Maximum Depth of Binary Tree
- Path Sum
Binary Search
When to Use:
- Sorted array/search space
- Finding specific value
- Finding first/last occurrence
- Optimization problems
Code Pattern:
let left = 0, right = arr.length - 1;
while (left <= right) {
let mid = Math.floor(left + (right - left) / 2);
if (arr[mid] === target) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
Complexity:
Time: O(log n)
Space: O(1)
Example Problems:
- Search in Rotated Sorted Array
- Find Peak Element
- Search a 2D Matrix
Dynamic Programming
When to Use:
- Optimization problems
- Overlapping subproblems
- Decision making at each step
- Count ways to do something
Code Pattern:
// Bottom-up approach
const dp = new Array(n + 1).fill(0);
dp[0] = baseCase;
for (let i = 1; i <= n; i++) {
// Build from smaller subproblems
dp[i] = f(dp[i-1], dp[i-2], ...);
}
return dp[n];
Complexity:
Time: O(n)
Space: O(n) or O(1)
Example Problems:
- Climbing Stairs
- House Robber
- Longest Common Subsequence
Backtracking
When to Use:
- Generate all combinations/permutations
- Constraint satisfaction
- Decision tree exploration
- Puzzle solving
Code Pattern:
function backtrack(path, choices) {
if (isSolution(path)) {
results.push([...path]);
return;
}
for (let choice of choices) {
if (isValid(choice)) {
path.push(choice);
backtrack(path, choices);
path.pop(); // backtrack
}
}
}
Complexity:
Time: O(n!)
Space: O(n)
Example Problems:
- Subsets
- Permutations
- N-Queens
HashMap/HashSet
When to Use:
- Counting frequencies
- Finding duplicates
- Grouping elements
- O(1) lookup needed
Code Pattern:
// Frequency counting
const freq = {};
for (let item of arr) {
freq[item] = (freq[item] || 0) + 1;
}
// Two Sum pattern
const seen = new Map();
for (let i = 0; i < arr.length; i++) {
if (seen.has(target - arr[i])) {
return [seen.get(target - arr[i]), i];
}
seen.set(arr[i], i);
}
Complexity:
Time: O(n)
Space: O(n)
Example Problems:
- Two Sum
- Group Anagrams
- Valid Anagram
šÆ Expanded Pattern Recognition Guide
Two Pointers Pattern
Two Sum II (Sorted Array)
Find two numbers that add up to target
3Sum
Find all unique triplets that sum to zero
Container With Most Water
Find max area between two lines
Trapping Rain Water
Calculate water trapped between bars
Remove Duplicates from Sorted Array
In-place duplicate removal
Sliding Window Pattern
Longest Substring Without Repeating Characters
Find longest unique substring
Minimum Window Substring
Find smallest window containing all chars
Longest Repeating Character Replacement
Max length with k replacements
Permutation in String
Check if s2 contains permutation of s1
Fruit Into Baskets
Max fruits with 2 types (subarray with 2 distinct)
š£ļø How to Communicate During Technical Interviews
Clarify the Problem
2-3 minutesRepeat the problem
"So if I understand correctly, we need to..."
Ask about constraints
"What's the size of the input? Can there be duplicates?"
Clarify edge cases
"What should I return if the array is empty?"
Confirm examples
"Let me trace through this example to make sure I understand"
Discuss Approach
5-7 minutesStart with brute force
"The naive approach would be... with O(n²) time"
Identify the pattern
"This looks like a two-pointer problem because..."
Optimize
"We can improve this by using a hashmap to..."
Analyze complexity
"This would give us O(n) time and O(1) space"
š§ Pattern Recognition Tips
Quick Pattern Identification Keywords
"Find pair/triplet that sum to X"
ā Two/Three Pointers
"Longest/shortest subarray/substring with condition"
ā Sliding Window
"Sorted array" + "find element"
ā Binary Search
"Generate all combinations/permutations"
ā Backtracking
"Maximum/minimum cost/path/sum"
ā Dynamic Programming
"Tree/graph traversal by levels"
ā BFS
"Find path/validate tree property"
ā DFS
"K-th largest/smallest/frequent"
ā Heap/Priority Queue
"Detect cycle in linked list"
ā Fast & Slow Pointers
"Group by property/find duplicates"
ā HashMap/HashSet