D
Dev Cookies
Guest
When solving coding problems, one of the most common confusions is whether a problem should be solved using recursion, backtracking, or dynamic programming (DP). Letβs break this down in a structured way so you can quickly identify the right approach during interviews or practice sessions.
Every DP is recursion + memoization/tabulation, but not every recursion is DP.
Ask yourself:
If only (1) β use recursion.
If (1) + (2) + (3) β use DP.
When you see a problem:
Being able to identify recursion vs DP comes with practice. Start with recursion, watch for repeated subproblems, and if you see themβupgrade to DP. In interviews, explicitly state your thought process; it shows depth of understanding even if the final code is not optimal.
Continue reading...
Step 1: Core Difference
- Recursion β Solves a problem by breaking it into smaller subproblems, each solved independently.
Dynamic Programming (DP) β A type of recursion where:
- Subproblems overlap (they repeat).
- The problem has optimal substructure (optimal solution depends on optimal solutions of subproblems).

Step 2: Checklist β Is it DP?
Ask yourself:
Can the problem be expressed recursively?
Example:Fib(n) = Fib(n-1) + Fib(n-2)
Do subproblems repeat? (Overlapping subproblems)
Example:Fib(5)
callsFib(4)
andFib(3)
, butFib(4)
again callsFib(3)
β repetition.
Does it have optimal substructure?
Example: Knapsack, LIS, shortest paths β best solution depends on smaller optimal solutions.


Step 3: Quick Heuristics
Problems usually solved with Recursion:
- Tree traversal (inorder, preorder, postorder)
- Backtracking (N-Queens, permutations, subsets)
- Divide & Conquer (merge sort, quick sort, binary search)
- Unique subproblems (no repetition)
Problems usually solved with DP:
- Fibonacci, Climbing Stairs
- Subset Sum, Knapsack, Partition problems
- Longest Common Subsequence (LCS), Edit Distance
- Grid path problems (unique paths, min path sum)
- Count / Min / Max / True-False optimization problems
Step 4: Example Comparison
Example 1: Fibonacci
- Recursive definition:
Fib(n) = Fib(n-1) + Fib(n-2)
- Problem:
Fib(3)
is computed multiple times β overlapping subproblems. Use DP.
Example 2: Tree Traversal
- Recursive definition: visit left β visit node β visit right.
- Subproblems are unique, no overlap.
DP not needed.
Step 5: Mental Flow
When you see a problem:
- Can I define it recursively? β If no, not recursion/DP.
- Am I recomputing the same subproblems? β If yes, move to DP.
- Is the problem about optimizing (min/max/count)? β Likely DP.
Rule of Thumb
- Exploring possibilities without repetition β Recursion / Backtracking.
- Optimizing or counting with overlapping subproblems β Dynamic Programming.
Quick Decision Tree
Code:
Problem β Can it be recursive?
β Yes
Are subproblems overlapping? β No β Use Recursion/Backtracking
β Yes
Does it have optimal substructure? β Yes β Use DP
Final Words
Being able to identify recursion vs DP comes with practice. Start with recursion, watch for repeated subproblems, and if you see themβupgrade to DP. In interviews, explicitly state your thought process; it shows depth of understanding even if the final code is not optimal.
Continue reading...