How to Recognize Which Algorithm to Use in an Interview
The #1 interview skill is pattern recognition. Learn the signals that indicate which algorithm to apply.
The problem statement ends. Implementation time. You know BFS, DFS, and DP. You have memorized the templates. But as you stare at the whiteboard (or the flickering cursor), one terrifying question paralyzes you: Which one do I use?
There are thousands of LeetCode problems, and none of them come with labels saying "Use Dynamic Programming here."
This momentary hesitation—the "Classification Gap"—is where interviews are lost. Passing isn't about knowing more algorithms; it's about rapidly recognizing which tool fits the lock in front of you.
The Pattern Recognition Framework
Every interview problem contains signals—keywords, constraints, and structures that point toward the right approach. Learning to spot these signals is the difference between confident problem-solving and panicked guessing.
Given a sorted array, find two numbers that sum to a target.
Signals in this problem:
Signal Words and Their Patterns
Certain phrases in problem statements are almost always associated with specific patterns. Here is a comprehensive guide:
| Signal | Pattern |
|---|---|
| "sorted array" + "find target" | Binary Search |
| "subarray" + "sum/length" | Sliding Window or Prefix Sum |
| "two elements" + "sum to target" | Two Pointers or Hash Map |
| "all permutations/combinations" | Backtracking |
| "shortest path" (unweighted) | BFS |
| "shortest path" (weighted) | Dijkstra |
| "number of ways" | Dynamic Programming |
| "maximum/minimum result" | DP (usually) or Greedy |
| "valid parentheses" or "matching" | Stack |
| "kth largest/smallest" | Heap |
| "connected components" | Union Find or DFS |
| "order of tasks" + "dependencies" | Topological Sort |
| "in-place" + "O(1) space" | Two Pointers |
The Trap: When Signals Lie
Keywords are powerful, but they can be traps. The B-tier candidate follows signals blindly; the A-tier candidate looks for the "Nuance Trigger."
Greedy vs. Dynamic Programming
Both handle "maximization" problems.
- The Trap: Assuming "max profit" implies Greedy immediately.
- The Check: Does the local best choice always lead to the global optimum? If one decision affects future availability (e.g., Knapsack), you must use DP.
Backtracking vs. DP
Both involve choices and states.
- The Trap: Seeing "find all ways" and thinking DP.
- The Check: Do you need to return the actual list of paths (Backtracking) or just the count/number (DP)? You rarely DP a list of lists.
BFS vs. DFS
Both traverse graphs and trees.
- The Trap: Using DFS for "shortest path" because it feels simpler.
- The Check: BFS guarantees shortest path in unweighted graphs because it explores level by level. DFS may find a path, but not the shortest. Use DFS for "all paths," "any path," or when you need to explore deeply before backtracking.
Binary Search vs. Two Pointers
Both work on sorted arrays.
- The Trap: Using Binary Search for "two sum in sorted array."
- The Check: Binary Search finds a single target value in O(log n). Two Pointers finds pairs or subarrays based on a condition in O(n). If you are searching for a relationship between two elements, Two Pointers is usually faster.
The Constraint Analysis Technique
Problem constraints are not just limits—they are hints. The input size tells you what time complexity is acceptable:
Constraint → Acceptable Complexity
The Question-Based Approach
When you are stuck, ask yourself these questions in order:
- Is the input sorted?
If yes, consider Binary Search or Two Pointers.
- Do I need all possibilities?
If yes, consider Backtracking.
- Is there a tree or graph structure?
If yes, consider DFS or BFS.
- Do I need to find a shortest path?
If yes, consider BFS (unweighted) or Dijkstra (weighted).
- Is there overlapping subproblems?
If yes, consider Dynamic Programming.
- Do I need fast lookup?
If yes, consider Hash Map.
- Do I need the min/max repeatedly?
If yes, consider Heap.
Worked Example: From Problem to Pattern
Theory is useful, but pattern recognition becomes real when you see it applied. Let us walk through the mental process on two actual interview problems.
Example 1: "Subarray Sum Equals K"
Given an array of integers and a target sum K, find the total number of continuous subarrays whose sum equals K.
Step 1: Signal Words
"Subarray" + "sum" screams Sliding Window or Prefix Sum.
Step 2: Check Constraints
n can be up to 20,000. This means O(n) or O(n log n) is required. Brute force O(n^2) will time out.
Step 3: Nuance Check
Sliding Window needs all positive numbers to work (shrinking the window must always decrease the sum). This problem allows negative numbers, so Sliding Window is broken.
Conclusion: Prefix Sum + Hash Map
Compute prefix sums. For each prefix sum, check if prefix_sum - K exists in a hash map. This gives O(n) time, O(n) space.
Example 2: "Number of Islands"
Given an m x n 2D grid of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.
Step 1: Signal Words
"Grid" + "connected" + "adjacent" screams Graph Traversal (BFS or DFS).
Step 2: Question-Based Approach
Do I need shortest path? No, we just need to count components. Both BFS and DFS work equally well here.
Step 3: Check Constraints
Grid is at most 300x300 = 90,000 cells. O(m * n) traversal is acceptable.
Conclusion: DFS (or BFS)
Iterate through the grid. When you find a '1', increment the count and flood-fill (DFS) to mark all connected land as visited. DFS is often simpler to implement for this kind of problem because it uses recursion instead of a queue.
Notice the pattern: read the problem for signals, check constraints for feasibility, then apply the nuance check to avoid traps. This three-step process becomes automatic with practice.
Practice Makes Pattern Recognition Automatic
Pattern recognition is a skill that develops with deliberate practice. The more problems you solve while consciously noting the pattern, the faster you will recognize them in the future.
The key is to study problems by pattern, not randomly. When you solve 5 Sliding Window problems in a row, the pattern becomes ingrained. When you solve random problems, patterns get muddled.
The 14 Core Patterns (Mental Shortcuts)
Linear Scanners
Tree & Graph Explorers
Optimizers
Structure Hacks
How TerminalTales Trains Pattern Recognition
TerminalTales organizes its entire curriculum around patterns. Each chapter focuses on a specific pattern, teaching you:
- When to use the pattern (the signals)
- How the pattern works (the mechanics)
- Why the pattern is efficient (the analysis)
- Multiple problems that use the pattern (the reinforcement)
By the time you finish, pattern recognition becomes automatic. You see a problem and immediately know which tool to reach for. If you are struggling with retention, see our guide on why you keep forgetting LeetCode solutions and how spaced repetition can help.
Master DSA with Story-Driven Learning
TerminalTales combines an immersive narrative with spaced repetition to help you actually remember what you learn. Start with 3 free chapters.
Start Learning Free