Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Cheat Sheets
Reference2 min read

Choosing a Pattern

A decision guide — map the phrases in a problem statement to the pattern that solves it.

Most interview problems are a known pattern wearing a costume. Before coding, match the statement against these cues — saying "this smells like X because Y" out loud is exactly what interviewers want to hear.

Cue → pattern

The problem says…Reach forSheet
"pair/triplet that sums to…", "have you seen this before?"Hash mapArrays & Hashing
Sorted array, pair from both ends, in-place dedupeTwo pointersTwo Pointers
"longest/shortest substring/subarray with property P"Sliding windowSliding Window
Sorted anything, "minimum X such that…", answer is monotonicBinary searchBinary Search
Matching brackets, undo/nesting, "next greater element"Stack / monotonic stackStacks
Reverse/reorder a list, cycle detection, k-th from endLinked-list pointersLinked Lists
Anything on a binary treeDFS recursion or BFS levelsBinary Trees
Prefix matching, autocomplete, word dictionaryTrieTries
"k largest/smallest/closest/most frequent", running medianHeapHeaps & Top-K
Ranges with start/end, "merge", "overlap", meeting timesIntervalsIntervals
"All combinations/permutations/ways to…" (n ≤ ~20)BacktrackingBacktracking
Grid regions, connectivity, prerequisites/dependenciesGraph BFS/DFS, topo sort, union-findGraphs
"Number of ways", "min cost to reach", choices with overlapDynamic programmingDynamic Programming
"Maximum you can schedule/keep", local choice feels safeGreedy (prove it!)Greedy
XOR, "appears once", powers of two, count bitsBit manipulationBit Manipulation

Tie-breakers

  • Substring vs subsequence: contiguous → sliding window / two pointers; non-contiguous → DP.
  • "All ways" vs "how many ways": enumerate them → backtracking; just count → DP.
  • Top-k vs k-th: keep a stream of answers → heap of size k; single k-th once → quickselect or heap.
  • Graph shortest path: unweighted → BFS; weighted non-negative → Dijkstra (Network Delay Time); ≤ k hops → Bellman-Ford style relaxation (Cheapest Flights).
  • Sorted input is a gift — it usually means two pointers or binary search; if you're not using the sortedness, you're probably missing the intended solution.

When you're stuck

  1. Restate inputs/outputs and solve a tiny example by hand — the manual process often is the algorithm.
  2. Say the brute force and its complexity. Then ask: what work is repeated? (→ DP/memo) What information would make each step O(1)? (→ hash map, stack, heap)
  3. Sort the input mentally — does the problem get easier?
  4. Check the constraints table in Big-O Complexity — the size of n narrows the pattern to two or three candidates.
Big-O ComplexityArrays & Hashing