Coin Change - Dynamic Programming Pattern

Coin Change in Java using the Dynamic Programming pattern: problem statement, approach, template, complexity, and interview tips.

Coin Change

Problem Statement

Find the fewest coins needed to make up an amount, or -1 if it cannot be made.

Pattern: Dynamic Programming

Break the problem into overlapping subproblems, store their answers, and combine them with a recurrence.

Use this pattern when a brute-force recursion repeats the same subproblems and the best answer builds on smaller best answers.

Approach

  1. Define what dp[i] (or dp[i][j]) means in one sentence.
  2. Write the recurrence and the base cases.
  3. Fill the table in the right order, then optimise space if possible.

Pattern Template (Java)

int[] dp = new int[n + 1];
dp[0] = 1; // base case
for (int i = 1; i <= n; i++) {
    dp[i] = /* combine earlier states using the recurrence */ 0;
}
return dp[n];

Adapt this template to the exact rules of Coin Change: decide what state you track, what condition moves the loop forward, and what the final answer is.

Complexity

Usually O(states x transitions) time; space can often be reduced to O(1) or one row.

Interview Tips

  • Start with the recursive solution, add memoization, then convert to a table.
  • Explain the state definition before the code; interviewers look for it.
  • Start with a brute-force idea, state its complexity, then explain how the Dynamic Programming pattern improves it.
  • Walk through a small example and at least one edge case (empty input, a single element, duplicates).

Continue Learning

Parent: DSA Interview Learning Path | Previous: House Robber | Next: Longest Common Subsequence

Was this guide helpful?

CodeWithVenu is free to read, with no paywall. If this guide saved you time or helped you prepare, a small voluntary contribution helps me write more practical tutorials, architecture guides, and interview-prep content.