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.

10 minutes
Intermediate
What you will learn

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.

TL;DR
Algorithm identification comes down to four diagnostic questions about the problem. Most platforms teach how algorithms work and how to apply them but skip the identification step entirely. This article gives you a repeatable process that works across every algorithm family.

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 means 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 engineers who solve novel problems from engineers who can only solve ones they've seen before. Most platforms assume identification develops passively through volume. For some engineers, 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.

The identification gap 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 most engineers haven't practiced crossing it 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.

Question 1: Ask what 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.

Question 2: Ask what 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.

Question 3: ask what 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.

Question 4: Ask what 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 isn't academic theory. It's how you confirm you've identified correctly.

“The four questions don't give you the answer. They give you a search space small enough that the answer becomes obvious.”
Algorithm identification

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 the identification: 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:

  1. 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:

Input
  • Trapping Rain Water
    Array of bar heights
  • Coin Change
    Array of denominations + target
  • Kth Largest Element
    Unsorted array + integer K
Output
  • Trapping Rain Water
    Single integer (total water)
  • Coin Change
    Single integer (minimum coins)
  • Kth Largest Element
    Single value
Transformation
  • Trapping Rain Water
    Compute bounded area from nearest taller bars on each side
  • Coin Change
    Optimal selection from repeated choices to reach target
  • Kth Largest Element
    Partial sort to isolate rank K
Identified pattern
  • Trapping Rain Water
    Monotonic stack (next/previous greater element invariant)
  • Coin Change
    DP (overlapping subproblems + optimal substructure)
  • Kth Largest Element
    Quickselect (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 most engineers misidentify problems

Misidentification 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. Engineers who skip question 1 fall into this trap repeatedly.
  • Greedy-vs-DP confusion: The difference between greedy and DP isn't complexity or difficulty. It's whether 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 identification 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.

⚠️ Warning
The costliest misidentification isn't picking the wrong algorithm. It's spending 15 minutes implementing the right algorithm applied incorrectly because you didn't verify the invariant. Question 4 exists to prevent exactly this.

Learning to identify which algorithm to use

Identification 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 practice identification 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 identification. 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. The identification muscle never fires.

Codeintuition is the only platform that teaches identification 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 recognition triggers that indicate 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 gives you identification training across 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. You didn't get smarter. You trained the one skill that every other platform assumed you'd figure out on your own.

Do you want to master data structures?

Try our data structures learning path made of highly visual and interactive courses. Get hands on experience by solving real problems in a structured manner. All resources you would ever need in one place for FREE

Start with the 4-question process from this article. Identify the input type (array, graph, tree, string), then the output type (single value, boolean, sequence), then the transformation (search, optimize, enumerate), and finally verify with the invariant. These four signals narrow the search space enough that the right pattern becomes identifiable even on completely novel problems.
They're related but distinct. Pattern recognition is the broader skill of noticing similarities between problems. Identification is the specific act of mapping a problem's constraints to a concrete algorithm or pattern. You can recognize that two problems feel similar without being able to name the pattern or explain why it works. The 4-question process closes that gap by making the reasoning explicit.
You can. Shuffle your problem queue so you don't know the topic before reading each problem. After reading, write down which pattern you think applies and why before checking the solution. This forces the identification step on every problem. The limitation is that without explicit identification training, you're building the skill through trial and error rather than direct instruction.
With deliberate identification practice, most engineers start recognizing triggers within 4 to 6 weeks of consistent work across diverse pattern types. The key is interleaving: mixing problems from different categories rather than solving 20 of the same type consecutively. Blocked practice builds speed on known patterns. Interleaved practice builds identification of unknown ones.
It covers algorithmic problems, which make up the majority of what FAANG companies test. System design, concurrency, and behavioral questions need different identification processes because the solution space is broader and constraints are more ambiguous. The habit of asking systematic diagnostic questions before jumping to implementation does transfer well to other technical contexts, though.
Was this helpful?