Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
PROBLEM-SOLVING TECHNIQUES

Dynamic Programming

Solve each subproblem once, store it, and build bigger answers from smaller ones.

Advanced14 problems4 interactive subtopicsO(amount × coins) time · O(amount) space
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (4)Open in full playground →
STEP 1/5Base case
∞ means “not reachable yet” (stored as 12)
dp
01234567891011
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
  • result
  • visited / done
  • active this step
  • read by this step
Space Play←→ StepR Replay
1 / 5
PYTHONcoins = [1, 2, 5], amount = 11
1def coin_change(coins, amount):
2 INF = amount + 1
▶3 dp = [0] + [INF] * amount
4 for a in range(1, amount + 1):
5 for c in coins:
6 if c <= a:
7 dp[a] = min(dp[a], dp[a - c] + 1)
8 return dp[amount] if dp[amount] != INF else -1
STEP 1 OF 5Setup

Base case

dp[0] = 0 coins. Every other amount starts at 12, which means “not reachable yet”.

VARIABLES

a
-
dp[1..5]
12, 12, 12, 12, 12
dp[11]
12
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

DP breaks a problem into overlapping subproblems, defines a state and a transition between states, and stores the results in a table (bottom-up) or a cache (top-down).

Why it matters

Recursion that recomputes the same states is exponential; storing them makes it polynomial.

Think of it like this

Climbing stairs: the ways to reach step n are the ways to reach n − 1 plus the ways to reach n − 2.

Use it when

  • Counting ways, or the min or max over choices
  • Optimal substructure plus overlapping subproblems
  • Sequences and strings - LCS, edit distance

Avoid it when

  • Choices never overlap - plain recursion or greedy is enough
  • You must list every solution - use backtracking

Complexity cheatsheet

OperationTimeSpace
1-D DP, e.g. climbing stairsO(n)O(n), or O(1) with two variables
2-D DP, e.g. LCS or edit distanceO(n × m)O(n × m)
0/1 knapsack with capacity WO(n × W)O(W) with a 1-D array

Often confused with: greedy - it commits to one choice; DP compares every choice using stored results.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

number of waysminimum costmaximum profitlongest subsequencecan you reach
  • n up to a few thousand often means O(n²) DP
  • If a brute-force recursion repeats the same arguments, memoize it
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • A wrong base case - for coin change dp[0] must be 0
  • Filling the table in an order that reads a state before it’s computed
  • Returning the “infinity” value instead of -1

Edge cases to test

  • amount = 0 - the answer is 0
  • No combination works, e.g. coins [2] and amount 3
  • A coin larger than the amount
Interview tip: Lead with the state and the transition: “dp[a] is the fewest coins for amount a, the min over coins of dp[a − c] + 1.”
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

What makes a problem a good fit for DP?

06

Practice

Apply what you just watched on real interview problems.

Generate Dynamic Programming problem in AI DSA Practice →
  • BeginnerClimbing StairsLeetCode 70 ↗ (opens in a new tab)
  • IntermediateCoin ChangeLeetCode 322 ↗ (opens in a new tab)
  • AdvancedEdit DistanceLeetCode 72 ↗ (opens in a new tab)
Greedy AlgorithmsBreadth-First Search