Skip to article content
FrontendXLesson 19 / 24

Dynamic programming foundations

Start with no coding-interview background, learn to trace an algorithm by hand, measure its cost, and finish able to reason about arrays, hashing, trees, graphs, dynamic programming and the trade-offs an interviewer expects you to name.

intermediate · about 40 minutes · 3 exercises

  • Predict
  • Explain
  • Implement

What you will be able to do

#
  • Identify overlapping subproblems and optimal substructure.
  • Convert a naive recursion into memoisation and then tabulation.
  • Define a state and a transition before writing code.

Overview

#

Two ingredients

Dynamic programming applies when a problem has overlapping subproblems (the same smaller problem is solved many times) and optimal substructure (the best answer is built from best answers to subproblems).

The naive recursion problem

snippet.txt
function fib(n) {
  if (n <= 1) return n;
  return fib(n - 1) + fib(n - 2);
}

This recomputes the same values exponentially many times, giving O(2^n). The subproblems overlap.

Memoisation (top-down)

Cache each result the first time you compute it:

snippet.txt
function fib(n, memo = new Map()) {
  if (n <= 1) return n;
  if (memo.has(n)) return memo.get(n);
  const result = fib(n - 1, memo) + fib(n - 2, memo);
  memo.set(n, result);
  return result;
}

Each value is computed once, so the time drops to O(n) at the cost of O(n) space.

Tabulation (bottom-up)

Fill a table from the smallest subproblem upward:

snippet.txt
function fib(n) {
  const dp = [0, 1];
  for (let i = 2; i <= n; i++) dp[i] = dp[i - 1] + dp[i - 2];
  return dp[n];
}

No recursion depth, and often the table can be reduced to a few variables, cutting space to O(1).

Define state before code

Before writing any DP, answer two questions in words: what does the state mean, and how does a state depend on smaller states? For Fibonacci the state is the value at index i and the transition is dp[i] = dp[i-1] + dp[i-2]. Getting the state right is most of the work.

Practice next

  • Solve the climbing-stairs problem with memoisation and with O(1) space.
  • Explain the state and transition for the coin-change problem.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    How many calls does the naive fib(5) make in total, including repeated subproblems?

    starter.javascript
    function fib(n) {
      if (n <= 1) return n;
      return fib(n - 1) + fib(n - 2);
    }

    Reveal the reference answer
    reference.javascript
    The call tree for fib(5) has 15 nodes in total, showing heavy recomputation. Memoisation reduces this to about 5 distinct computations.

    Authored expected output: 15

    The exponential blow-up comes from recomputing the same subproblems.

  2. Exercise 2Implement

    Implement fib(n) bottom-up using O(1) extra space.

    starter.javascript
    function fib(n) {
      // your code here
    }

    Reveal the reference answer
    reference.javascript
    function fib(n) {
      if (n <= 1) return n;
      let prev = 0, curr = 1;
      for (let i = 2; i <= n; i++) {
        const next = prev + curr;
        prev = curr;
        curr = next;
      }
      return curr;
    }

    Authored expected output: 8 for fib(6)

    Only the last two values are needed, so two variables replace the whole table.

  3. Exercise 3Explain

    State the two properties a problem must have for dynamic programming to apply.

    Reveal the reference answer
    reference.text
    First, overlapping subproblems: the same smaller problem is solved repeatedly, so caching helps. Second, optimal substructure: the best answer for the whole problem can be built from the best answers to its subproblems. Without both, a different technique is usually better.

    Authored expected output: Overlapping subproblems and optimal substructure.

    These two properties are the test for whether DP is the right tool.

Common misconceptions

#
  • Any recursion is dynamic programming. DP requires overlapping subproblems; otherwise it is just divide and conquer.
  • Memoisation changes the answer. It only avoids recomputation; the result is identical.
  • Bottom-up is always better. It avoids recursion depth but can compute states you never need.

Follow-up practice

#
  • dsa-from-zero-dp-on-grids-and-sequences

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.