Pattern Studioalgorithmic thinking
All patterns

Dynamic Programming

Dynamic Programming
Intermediate
Interactive

Choose a state representation, then build larger answers from solved states.

Time
Depends on the state space
Space
Depends on stored states
ActiveComparingMatch / bestIn windowFrontierVisitedExcludedAnswer
No frames to display.

The idea

Dynamic programming becomes easier when you first name the state representation. 1D states use one progress index, Grid states use two coordinates, Knapsack states track a reusable budget, and Digit States build bounded numbers position by position.

Analogy

This visualization makes the state transitions behind dynamic programming explicit, one decision at a time.

When to reach for it

  • The problem exhibits this pattern's defining invariant.
  • A direct brute-force approach repeats work or explores unnecessary choices.
  • You need a standard interview-ready template.

Language-independent template

define the state representation and base cases
iterate in dependency order
transition from already-solved states

Complexity — time Depends on the state space, space Depends on stored states.

Common mistakes

  • Choosing the wrong state or invariant.
  • Updating state before preserving the value needed next.
  • Missing a boundary or base case.

Practice progression

Canonical interview problems, ordered from foundation to advanced application.

Easy

Count distinct ways to reach the top using one- or two-step moves.

Medium

Find the minimum insertions, deletions, and replacements to transform one string.

Hard

Choose a bursting order that maximizes collected coins.

Variations

Change the input to exercise another path.
Adapt the invariant to a related optimization or counting problem.