Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Backtracking and Dynamic Programming (Summary)

You’ve practiced two patterns for problems built from a sequence of choices:

  • Backtracking builds every candidate one choice at a time with a shared path, undoes each choice with a matching pop(), and records a copy when a candidate is complete. Pruning partial answers that can’t work, and building each combination in one order, keeps the search close to the size of its output.
  • Dynamic programming names the smaller question that keeps coming up, answers it once, and builds bigger answers from a Map or a table. The cost is the number of states times the work per state, which turns exponential searches into polynomial ones.

Things to Remember in an Interview

  • Ask first whether the problem wants every option or just a count or best value. Listing calls for backtracking, while counting and optimizing usually call for dynamic programming.
  • Say the state out loud before you code: “the fewest stamps for exactly this amount” or “the routes into this cell.”
  • Push [...path], not path, and key a Set or Map with strings or numbers, never with arrays.
  • Start counts at 1n when they can pass Number.MAX_SAFE_INTEGER, use Infinity for impossible states, and build 2D tables with Array.from().
  • Prefer a loop over deep recursion when the input can be thousands of steps long.

Keep Practicing

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.