DSA Trainer
← All patterns

How to know which LeetCode pattern to use

You understand each pattern on its own. Then a fresh problem loads and you go blank. Which one is this? That gap is the whole game. The skill isn't knowing what a sliding window is; it's noticing the problem in front of you is a sliding window problem. This page maps the signals: read the clue words in the problem, find the row, click through to learn the pattern.

Arrays & strings

PatternClue words in the problemReach for it when…
Hash Map
sums to a targethave I seen this valuecount occurrencescomplementlook it up by valuegroup by a shared key
You need to find the complement or pair of a current value
Set
duplicateall uniqueseen beforealready visiteddoes it existdistinct values
You need to detect duplicates in an array or string
Frequency Map
how many times eachanagramrearrange the letterssame charactersappears most oftencount of each character
You need to check if two collections have the same elements regardless of order
Two Pointers
sorted arrayfind a pair that sums tocompare both endspalindromemove inwardin place, no extra space
You need to compare elements at two ends of an array or string
Sliding Window
contiguous subarraysubstringlongest or shortestat most kwithout repeatingwindow of size k
You're looking at a subarray or substring of the input
Prefix Sum
sum of a rangesubarray sums to kcumulative / running totalsum between i and jnumber of subarrays with sum
You need the sum of elements between two indexes, potentially many times
Kadane's Algorithm
maximum sum subarraycontiguouslargest sumrunning totalmust pick at least one
You need the maximum (or minimum) sum of a contiguous subarray
Tracking Minimum
maximum profitlowest seen so farbuy low, sell highlargest gap where later beats earliersingle pass
You need the best value seen before the current position
Intervals
intervalsoverlapping rangesmerge themstart and end timesscheduling conflictnon-overlapping
You're working with [start, end] pairs that may overlap
Cyclic Sort
numbers 1 to nvalues in a fixed rangefind the missing numberfind the duplicatesmallest missing positivewithout extra space
The input contains numbers in the range 1 to N where N is the array length
Sorting Algorithms
sort the inputascending or descending orderarrange bykth largest after sortingrank / orderrearrange
The problem becomes much simpler if the input is in order

Stacks & queues

PatternClue words in the problemReach for it when…
Stack
matching parenthesesbalanced bracketsvalidnestedmost recently seeninnermost first
You need to match opening and closing pairs like brackets or tags
Monotonic Stack
next greater elementnext smaller elementnearest warmer day to the rightspan until a bigger valueprevious smaller element
You need the next element that is larger (or smaller) than the current one
Monotonic Queue
maximum in every windowminimum in every windowsliding window of size kbest in the current windowdouble-ended queue
You need the maximum or minimum of a sliding window of fixed size k

Searching

PatternClue words in the problemReach for it when…
Binary Search
sorted inputfind target in O(log n)first or last positionrotated sorted arrayminimum value that workspeak element
The input is sorted (or the answer space is ordered monotonically)
Divide and Conquer
split in halfsolve each halfcombine the resultsmergekth element by partitioningcount across halves
The problem can be split into independent subproblems of the same type

Linked lists

PatternClue words in the problemReach for it when…
Linked List
linked listnode with a next pointerreverse the listmerge two listsdetect a cyclefind the middle
You're working with nodes connected by next pointers
Fast & Slow Pointers
cycleloopcircularfind the middle in one passwhere the cycle startswithout extra space
You need to detect a cycle in a linked list or number sequence

Trees & graphs

PatternClue words in the problemReach for it when…
Tree Traversal
binary treeroot and childrendepth or heightlevel by levelinorder / preorder / postordersubtree
You need to visit every node in a binary tree
Graph Traversal
nodes and edgesconnectedis there a pathnumber of islands / regionsshortest pathvisit neighbors
You're exploring connected regions in a grid or graph
Matrix Traversal
grid2D matrixadjacent cells4-directionalregions in a gridrows and columns
The input is a 2D grid and you need to explore connected regions
Topological Sort
prerequisitesmust come beforevalid order to finishdependency orderingdirected acyclic graphbuild / compile order
Tasks have prerequisites, some must be completed before others can start
Union Find
connected componentsare these in the same groupnumber of groupsmerge two groupsredundant edgedynamic connectivity
You need to group elements into connected components dynamically
Trie
prefixstarts withautocompletesearch words in a dictionarylongest common prefixword search
You need to search for strings by prefix, autocomplete, word suggestions

Recursion, backtracking & DP

PatternClue words in the problemReach for it when…
Recursion
defined in terms of itselfsubproblemexplore every branchnested structurenth termbreak into smaller pieces
The problem naturally breaks into a smaller version of itself
Memoization
overlapping subproblemsthe same call repeatscount the waysminimum or maximumnth valuecache the results
A recursive solution is correct but too slow because it recomputes the same inputs
Dynamic Programming
how many waysminimum or maximum to reachcan you reach the targetcount the pathseach step is a choiceoverlapping subproblems
The brute-force recursion recomputes the same subproblem multiple times
Backtracking
all possibleevery combinationevery permutationgenerate allfind all validsatisfies the constraints
You need to generate all subsets, combinations, or permutations of an input
Greedy
minimum number ofmaximum you cancan you reach the endfewest stepspick the best at each stepafter sorting
Each step has a clear locally optimal choice that doesn't undermine future steps

Heaps & merging

PatternClue words in the problemReach for it when…
Heap / Priority Queue
k largest or smallestkth largesttop krunning medianappears most frequentlyfrom a stream
You need the k largest or k smallest elements from a collection
Two Heaps
running medianbalance two halvesfrom a stream of numbersmax of the low half, min of the high halfschedule by two priorities
You need the median of a dynamically growing stream of numbers
K-way Merge
k sorted listsk sorted arraysmerge sorted inputskth smallest acrosssorted matrixcombine sorted streams
You have K sorted lists or arrays and need to merge them into one sorted output

Bit manipulation

PatternClue words in the problemReach for it when…
Bit Manipulation
appears once while others repeatno extra memorypower of twocount the set bitsXORbinary representation
You need to find a unique element in a sea of duplicates

Stuck between two that feel similar?

The hardest part is telling apart patterns that look alike. These break down the exact difference:

Train the recognition, not just the patterns

150guided problems. Five are free, no account needed. Each one walks you from “what is this asking?” to the pattern to the code, with hints that nudge, not spoil.