Skip to content
DSA patterns 9 min read

DSA Problem-Solving Patterns: A Complete Overview

A problem-solving pattern is a reusable shape of solution that recurs across hundreds of different-looking problems. Once you can match a new problem to a pattern, you stop solving from scratch and start adapting a template you already trust. This page is the map: it names the major patterns, tells you the signal in a problem statement that triggers each one, and links to a dedicated recap page for every pattern.

Think of patterns as the difference between “I’ve never seen this” and “this is just two-sum on a sorted array.” The problems are infinite; the patterns are few.

How to recognize a pattern

Most patterns reveal themselves in the input shape, the thing being asked, and the target complexity. Train yourself to read a problem and ask three questions:

  1. What is the input? (sorted array? linked list? grid? string? a numeric range?)
  2. What am I optimizing? (a contiguous chunk? a pair? a count of arrangements? a min/max?)
  3. What complexity is implied? (an O(n log n) budget screams sorting or binary search; O(n) over a contiguous range screams sliding window.)

The table below is your cheat sheet.

PatternSignal in the problemTypical complexityRecap page
Two PointersSorted array/string; find a pair/triplet; reverse or partition in placeO(n) time, O(1) spaceTwo Pointers
Sliding WindowContiguous subarray/substring; “longest/shortest/at most k”O(n) time, O(1)–O(k) spaceSliding Window
Fast & Slow PointersLinked list or sequence; cycle, middle, or nth-from-endO(n) time, O(1) spaceFast & Slow Pointers
Binary SearchSorted data, or a monotonic yes/no over a numeric answer rangeO(log n) per searchBinary Search
BacktrackingGenerate all subsets/permutations/combinations; constraint puzzlesExponential (pruned)Backtracking
Dynamic ProgrammingOverlapping subproblems + optimal substructure; count/min/max of choicesOften O(n·m)Dynamic Programming

A worked recognition example

Consider: “Given a sorted array, return two indices whose values sum to a target.” The word sorted plus find a pair points straight at two pointers — walk one pointer from each end, moving inward based on whether the current sum is too big or too small. Here is the template that pattern produces:

#include <vector>
// Two pointers on a sorted array: find a pair summing to target.
std::vector<int> twoSumSorted(const std::vector<int>& a, int target) {
    int lo = 0, hi = (int)a.size() - 1;
    while (lo < hi) {
        int sum = a[lo] + a[hi];
        if (sum == target) return {lo, hi};
        if (sum < target) lo++;        // need a bigger sum
        else hi--;                     // need a smaller sum
    }
    return {-1, -1};                   // no pair found
}

For beginners: You don’t need to memorize every pattern at once. Learn one pattern, solve five problems with it, and it becomes automatic. The signal column above is what you scan first when reading any new problem.

Where patterns connect to the core topics

Patterns aren’t separate from the data structures — they grow out of them. The two-pointers pattern is the searching topic’s two-pointers technique. The fast & slow pattern is the linked-list topic’s fast & slow pointers. The binary-search pattern generalizes binary search into binary search on the answer. Backtracking is structured recursion, and DP patterns build on the dynamic programming topic.

Going deeper: Many hard problems are combinations — e.g. binary search the answer, then verify each candidate with a greedy or sliding-window scan. Recognizing the outer pattern first, then the inner one, is how experienced solvers decompose intimidating questions.

How to use the rest of this section

Each following page is a focused recap: the signal, a reusable 4-language template, the gotchas, and links to the topic pages and exercises where you can drill it. Read them in order, or jump to whichever pattern a problem in front of you seems to need.

Next: start with the two-pointers pattern.

Last updated June 25, 2026
Was this helpful?