What is the two pointers pattern?
Using two pointers that move through the data structure (often from opposite ends or same end with different speeds) to solve problems in linear time. Common with sorted arrays and linked lists.
Give three example problems that use the two pointer pattern.
1) Two Sum II (sorted array), 2) Remove duplicates from sorted array, 3) Container with most water, 4) Valid palindrome, 5) Three sum
What is the sliding window pattern?
Maintaining a ‘window’ that slides over the data, expanding or contracting based on conditions. Used for substring/subarray problems with contiguous elements.
What are the two types of sliding window problems?
Fixed-size window (e.g., max sum of k consecutive elements) and variable-size window (e.g., smallest subarray with sum >= target).
Give three example problems that use the sliding window pattern.
1) Maximum sum subarray of size k, 2) Longest substring without repeating characters, 3) Minimum window substring, 4) Longest substring with k distinct characters
What is the fast and slow pointers pattern?
Using two pointers moving at different speeds through a linked list or array. The fast pointer moves faster (usually 2x), creating a predictable relationship between their positions.
Give three example problems that use fast and slow pointers.
1) Linked list cycle detection, 2) Find the middle of linked list, 3) Palindrome linked list, 4) Find the start of a cycle, 5) Happy number
What is the merge intervals pattern?
Dealing with overlapping intervals by sorting and then merging or processing them. Often involves comparing end of one interval with start of next.
Give three example problems that use the merge intervals pattern.
1) Merge overlapping intervals, 2) Insert interval, 3) Meeting rooms (can a person attend all?), 4) Meeting rooms II (min rooms needed)
What is the cyclic sort pattern?
When given an array containing numbers in a range (e.g., 1 to n), place each number at its correct index by swapping. Useful for finding missing/duplicate numbers.
Give three example problems that use the cyclic sort pattern.
1) Find the missing number, 2) Find all missing numbers, 3) Find the duplicate number, 4) Find all duplicates
What is the in-place linked list reversal pattern?
Reversing a linked list or portion of it without extra space by manipulating pointers. Track previous, current, and next nodes.
What is the template for reversing a linked list in-place?
prev = null, curr = head. While curr: save next = curr.next, reverse pointer curr.next = prev, advance prev = curr, curr = next. Return prev.
What is the tree BFS pattern?
Level-order traversal using a queue. Process all nodes at current level before moving to next level. Useful for level-based problems.
Give three example problems that use tree BFS.
1) Level order traversal, 2) Zigzag traversal, 3) Level averages, 4) Minimum depth of tree, 5) Right side view of tree
What are the three types of tree DFS traversals?
Pre-order (root, left, right), In-order (left, root, right), Post-order (left, right, root). Each has different use cases.
When would you use in-order traversal?
In-order traversal of a BST visits nodes in sorted order. Useful for finding kth smallest element or validating BST property.
When would you use post-order traversal?
When you need to process children before parents: calculating tree height, deleting trees, evaluating expression trees.
What is the two heaps pattern?
Using a max-heap and min-heap together to track a dynamic dataset, often for finding the median or maintaining a division of elements.
How do you find the median of a stream using two heaps?
Max-heap stores smaller half, min-heap stores larger half. Balance sizes (differ by at most 1). Median is top of larger heap or average of both tops.
What is the backtracking pattern?
Building solutions incrementally, abandoning a partial solution (‘backtracking’) as soon as it’s determined it can’t lead to a valid solution. Explores all possibilities systematically.
What is the template for backtracking?
Base case: add valid solution. For each choice: 1) make the choice, 2) recurse, 3) undo the choice (backtrack). Often track ‘used’ elements.
Give three example problems that use backtracking.
1) Subsets, 2) Permutations, 3) Combination sum, 4) N-Queens, 5) Sudoku solver, 6) Word search, 7) Generate parentheses
What is the modified binary search pattern?
Adapting binary search for non-obvious applications: searching in rotated arrays, finding boundaries, searching in infinite/unknown-size arrays.