How to Identify Which Algorithm to Use
Learn how to identify which algorithm to use with a 4 question framework that works across arrays, graphs, DP, and trees.
Why algorithm identification is a distinct skill most platforms never teach
A 4 question framework for selecting the right pattern
How the framework applies across five different problem types
How to train identification deliberately through interleaved practice
Two engineers sit down with the same unfamiliar problem. One stares at the description, mentally cycling through every technique, trying each until something sticks. The other reads three lines, notices the input is a directed graph with dependencies, and knows topological sort applies within 30 seconds. Both have solved roughly the same number of problems. Both have been preparing for months. One has trained how to identify which algorithm to use. The other is still guessing.
The gap isn't talent or problem count. It's a skill that almost nobody trains directly.
The phase every platform skips
Every DSA learning resource teaches two things. How an algorithm works (the mechanism) and how to apply it (the problems). What almost none of them teach is the step in between, where you look at an unfamiliar problem and determine which algorithm actually applies.
Algorithm identification: Reading a problem and systematically determining which pattern fits before you write any code. You analyze four diagnostic signals: the input, the output, the required transformation, and the algorithmic invariant that enables it.
This is what separates solving novel problems from only solving ones you've seen before. Most platforms assume this skill develops passively through volume. Sometimes it does develop that way. Someone who solves 500+ problems across diverse categories will start recognizing patterns by feel. But that's an expensive way to build the skill, like learning to read by staring at enough words until the alphabet clicks. It works for a fraction of learners and takes way too long for most.
This missing skill shows up during interviews in a specific, painful way. You've studied topological sort in depth. You understand how it works. You can implement it when someone tells you to use it. But the problem description doesn't say "use topological sort." It says "determine if all courses can be completed given their prerequisites." The bridge between those two sentences is identification, and that bridge rarely gets practised deliberately.
Four questions to identify which algorithm to use
The process is four questions, asked in order. Each one narrows the search space until the right pattern becomes clear.
- 1What kind of input are you given?
Is it a sorted array, an unsorted array with a contiguous element constraint, a tree, a directed graph, or a string? The input type eliminates entire algorithm families immediately. Directed acyclic graphs point toward topological sort or DP on DAGs. Sorted arrays with a target point toward binary search. Strings with a "contiguous range" constraint point toward sliding window. - 2What does the expected output look like?
Is the output a single value like a count or minimum, a boolean, a sequence, a subset, or a transformation of the input? Output type tells you what kind of computation you need. "Find the minimum number of X" usually signals DP or BFS. "Determine if X is possible" often signals DFS, backtracking, or DP. "Return all valid X" signals backtracking. - 3What transformation connects input to output?
What operation turns the input into the desired output? Are you searching, sorting, partitioning, traversing, optimizing, or enumerating? This narrows the search from algorithm families to specific patterns. - 4What algorithmic invariant enables that transformation?
The invariant is the why behind the pattern. For two pointers on a sorted array, the invariant is that moving one pointer eliminates a provably suboptimal portion of the search space. For BFS on an unweighted graph, the invariant is that the first time BFS reaches a node, it's found the shortest path. The invariant is how you confirm you've identified correctly, not academic theory.
“The four questions don't give you the answer. They give you a search space small enough that the answer becomes obvious.”
Identifying which algorithm to use: Five problems
Five problems across five different algorithm families, with all four questions applied each time.
Take Course Schedule, which asks whether you can finish all courses given their prerequisites. Your input is a directed graph where courses are nodes and prerequisites are edges. You need a boolean output, whether everything can be completed. The transformation requires determining whether a valid ordering of all nodes exists. And the invariant confirms your reading: a valid ordering exists if and only if the graph contains no cycle. Topological sort on a DAG produces exactly such an ordering.
Once you've identified the pattern, the implementation follows directly from the invariant:
Python
You don't need to have seen Course Schedule before. You need to recognize "directed dependencies + valid ordering = topological sort."
Now consider Longest Substring Without Repeating Characters. Your input is a string with characters as elements, and you need a single integer representing maximum length. Finding the longest contiguous range satisfying a uniqueness constraint is the transformation. The invariant confirms it: the variable sliding window maintains a window where all characters are unique, expanding when the constraint holds and contracting when it breaks.
The trigger words are "contiguous range" and "optimize length." Those two together point to sliding window before you've finished reading the problem description. Three more problems diagnosed the same way:
- Trapping Rain WaterArray of bar heights
- Coin ChangeArray of denominations + target
- Kth Largest ElementUnsorted array + integer K
- Trapping Rain WaterSingle integer (total water)
- Coin ChangeSingle integer (minimum coins)
- Kth Largest ElementSingle value
- Trapping Rain WaterCompute bounded area from nearest taller bars on each side
- Coin ChangeOptimal selection from repeated choices to reach target
- Kth Largest ElementPartial sort to isolate rank K
- Trapping Rain WaterMonotonic stack (next/previous greater element invariant)
- Coin ChangeDP (overlapping subproblems + optimal substructure)
- Kth Largest ElementQuickselect (partition invariant isolates Kth position)
One diagnostic process produced five different outputs depending on the problem. The questions stayed constant while the answers changed every time.
Why problems get misidentified
Picking the wrong algorithm happens for specific, predictable reasons.
- Surface similarity traps: Two problems can look nearly identical in their descriptions but require completely different algorithms. "Find the shortest path" in an unweighted graph calls for BFS. Add positive edge weights and the answer becomes Dijkstra. Introduce negative edges and now you need Bellman-Ford. The surface phrase is identical each time, but the input characteristics (weighted or unweighted, negative edges or not) determine the answer. Skipping question 1 means falling into this trap repeatedly.
- Greedy vs DP confusion: Greedy and DP differ on one question: do locally optimal choices produce a globally optimal result? Greedy works when the problem has the greedy choice property, meaning picking the best option at each step never requires backtracking. DP is needed when it doesn't. Activity selection is greedy because earliest finish time always works. The 0/1 knapsack is DP because picking the highest value to weight ratio first can miss the optimal combination. Question 4 addresses this directly, because if you can't prove the greedy invariant, you likely need DP. For the DP specific version of this diagnostic process, see how to identify if a problem requires DP.
- Output type mismatch: This one catches people off guard. An engineer sees "find all valid combinations" and reaches for DP because they've used DP for counting problems. But DP finds optimal values or counts. Enumerating all valid results is backtracking territory. Question 2 catches this before you waste 15 minutes building a DP solution that can't return what the problem actually asks for.
These traps get worse under time pressure. It's tempting to skip the diagnostic step and jump straight to the first pattern that feels right during an interview. That instinct is completely understandable, but it's also the most common source of wrong starts under a clock.
When the framework points to more than one pattern
Sometimes you'll run through all four questions and end up with two plausible answers instead of one. That's not a failure of the process. It's a signal that the problem sits at the boundary between pattern families, and boundary problems are some of the most common ones interviewers pick.
Consider a problem that asks you to find the shortest transformation sequence from one word to another, changing one letter at a time. The first three questions are straightforward: word list input, single integer output, search through intermediate states. But question 4 is where it gets interesting: you could model this as BFS on an implicit graph where each word is a node, or you could frame it as DP where each state represents a word and you're minimizing transitions.
Both framings are technically valid. BFS wins here because the graph is unweighted and you want the shortest path, which is exactly what BFS guarantees. The DP framing would work but adds unnecessary complexity. When two patterns survive the four questions, break the tie with a simpler rule: which invariant maps more directly to what the problem asks?
This happens more often than you'd expect. Tree problems occasionally straddle DFS and BFS depending on whether you need level order information. The four questions don't always produce a single answer on the first pass. But choosing between two candidates you understand is fundamentally different from choosing among fifteen you haven't filtered.
Two patterns can coexist.
Some problems genuinely require combining two patterns. Certain graph problems need both BFS and union find at different stages. When you hit these hybrid problems, the framework still helps because you've identified which patterns are in play. You're not guessing anymore. You're deciding how to compose known tools.
Recognizing when you've picked wrong
The other thing worth practising is noticing during implementation that your identification was off. A recurrence relation that keeps getting more complex instead of simplifying suggests you might've picked DP when greedy was the right call. A DFS tracking so much state that the function signature has five parameters points toward a different traversal strategy. And when your sliding window's contraction logic doesn't converge, the constraint might not be monotonic, which means sliding window doesn't apply.
Catching a wrong identification at minute 5 is recoverable. Catching it at minute 25 usually isn't. Check for these warning signs after writing the first 10 lines of code, not after finishing the full implementation.
Learning to identify which algorithm to use
This skill doesn't have to develop passively over years of problem solving. You can train it directly, the same way you train the algorithms themselves.
The key insight from learning science is interleaving: mixing practice across different pattern types so you're forced to figure out which pattern applies on every problem. Blocked practice (solving 20 sliding window problems in a row) trains application. Interleaved practice (mixing sliding window, two pointers, DP, and graph problems) trains the selection step. Studies on interleaved math and motor skill practice show the same result: worse performance during training, but significantly better transfer to novel problems.
This is the gap most platforms don't address. LeetCode's default practice mode lets you filter by topic, which is blocked practice. You already know it's a sliding window problem before you read the description. You never have to decide which pattern to use.
Codeintuition is the only platform that teaches this as a separate phase. Every pattern module includes a dedicated identification lesson before any problems begin. You don't learn what the variable sliding window is and then jump to problems. You first learn the cues that tell you when a variable sliding window applies, such as "contiguous range" + "dynamic constraint" + "optimize length or count." Then you practice applying the pattern to problems where you've already been trained to recognize it.
The learning path sequences 75+ patterns this way across 16 courses. Premium covers the full path at $79.99/year, but even the free Arrays course teaches you to recognize 8 patterns covering two pointers, simultaneous traversal, fixed sliding window, variable sliding window, interval merging, and maximum overlap. After completing it, you'll recognize triggers on unfamiliar problems before reaching for the hints.
Six months ago, you would've stared at Course Schedule and tried BFS, then DFS, then maybe DP, burning 25 minutes before arriving at topological sort by elimination. Now you read "directed dependencies" and "valid ordering" and the pattern is clear before you've finished the description. What changed was training the one skill that every other platform assumed you'd figure out on your own.
Want to learn the identification triggers for each pattern?
Codeintuition's learning path teaches a dedicated identification lesson before every pattern's problems begin. Try the 4 question process on sliding window and two pointers with the FREE Arrays course