Skip to content
← Back to Blog

Pattern Combinations: When You Need Two Patterns

11 min read

Most problems map to a single pattern. But some require combining two or more. Recognizing these combinations separates strong candidates from those who get stuck. This guide covers common pattern combinations with real problem examples.

Binary Search + Another Pattern

Binary Search often pairs with another pattern when the problem asks for the "minimum X such that condition Y holds" or "maximum X such that condition Y holds." You binary search on the answer space and use another pattern to check feasibility.

Koko Eating Bananas (LeetCode 875)

Find the minimum eating speed so Koko can finish all bananas in H hours. Binary search on speed; for each speed, simulate eating (a simple loop) to check if it is feasible. Pattern: Binary Search + Simulation.

Split Array Largest Sum (LeetCode 410)

Split array into K subarrays such that the largest subarray sum is minimized. Binary search on the maximum sum; use a greedy scan to check if we can split within that limit. Pattern: Binary Search + Greedy.

Capacity to Ship Packages (LeetCode 1011)

Minimum ship capacity to deliver all packages in D days. Binary search on capacity; greedy scan to count days needed. Pattern: Binary Search + Greedy.

View Binary Search Pattern →

BFS + DFS

Some graph problems need both traversals. BFS for shortest path or level order; DFS for connectivity, cycles, or exploring all paths.

Number of Islands (LeetCode 200)

Count connected components. DFS (or BFS) to explore each island. Either works; DFS is slightly simpler for "mark and explore." Some variants combine: use BFS from a start node, then DFS to mark visited.

Word Ladder II (LeetCode 126)

Find all shortest transformation sequences. BFS to find the shortest distance; then DFS (or reverse BFS) to reconstruct all paths. BFS gives the structure; DFS enumerates paths.

Hash Map + Sliding Window

Sliding window problems often need a hash map to track window state: character frequencies, element counts, or last-seen indices.

Longest Substring Without Repeating Characters (LeetCode 3)

Sliding window with a hash map storing each character's last index. When you see a repeat, jump left past the previous occurrence. Pattern: Sliding Window + Hash Map.

Minimum Window Substring (LeetCode 76)

Variable-size window; hash map for frequency of characters in t. Expand right, shrink left when all characters are covered. Pattern: Sliding Window + Hash Map.

Substring with Concatenation of All Words (LeetCode 30)

Sliding window over s; hash map to track which words from the list are in the current window and their counts. Pattern: Sliding Window + Hash Map.

Two Pointers + Hash Map

When you need to find pairs or subarrays with a property, Two Pointers (for sorted data) or Hash Map (for unsorted) can combine with other techniques.

3Sum (LeetCode 15)

Fix one element; use Two Pointers for the remaining two. Pattern: Sort + Two Pointers. (Hash Map variant exists but is trickier with duplicates.)

4Sum II (LeetCode 454)

Split into two pairs: hash map for sums of (A[i], B[j]); iterate over (C[k], D[l]) and look up -(C[k]+D[l]). Pattern: Hash Map + Iteration.

View Two Pointers Pattern →

Heap + Hash Map

Top K problems often need a heap for the K largest/smallest and a hash map for frequency or tracking.

Top K Frequent Elements (LeetCode 347)

Hash map to count frequencies; min-heap of size K to keep top K frequent. Pattern: Hash Map + Heap (or QuickSelect).

LFU Cache (LeetCode 460)

Hash map for key→value; another structure (e.g., doubly linked list per frequency) for eviction. Combines hashing with frequency ordering. Pattern: Hash Map + Custom Data Structure.

View Top K Pattern →

Backtracking + Memoization (DP)

Some problems have a natural recursive structure. Naive backtracking is exponential; adding memoization turns it into DP.

Word Break (LeetCode 139)

Try each prefix; if it is in the dictionary, recurse on the rest. Memoize by start index. Pattern: Backtracking + Memoization.

Partition Equal Subset Sum (LeetCode 416)

For each element, include or exclude. Memoize (index, remaining sum). Pattern: Backtracking + Memoization = 0/1 Knapsack.

How to Spot Pattern Combinations

  • Two-phase structure: Does the problem have a clear "search for X" and "verify X"? Think Binary Search + verification pattern.
  • Optimization over structure: Do you need to traverse a structure (BFS/DFS) and also optimize something (path length, count)? Combine traversal with DP or greedy.
  • State tracking: Does your main pattern need to track counts or frequencies? Add a hash map.
  • Recursion with overlap: If backtracking has overlapping subproblems, add memoization.

Related reads:

Master individual patterns first

Pattern combinations make sense once you know each pattern in isolation. Browse our 29 patterns to build your foundation.

Browse All Patterns