Question presented to candidate: "What is dynamic programming? Explain it in your own words, then show me in Python how you would turn a slow recursive Fibonacci into a dynamic-programming solution, and tell me when you would reach for DP at all."
What a strong answer should cover:
- DP = solve each overlapping subproblem once and reuse the stored answer; it needs overlapping subproblems and optimal substructure.
- The two styles: memoization (top-down: recursion + cache) and tabulation (bottom-up: fill a table in dependency order), plus the rolling-variable space optimisation.
- The five-step recipe: state, recurrence, base cases, order, answer β and the running-time formula states Γ work per state.
- Why plain recursion is exponential (about 2.7 million calls for fib(30)) and DP is O(n).
- Python specifics: a
dictorfunctools.cacheas the memo, the 1000-frame default recursion limit that breaks deep memoized recursion, arbitrary-precisionint, and the mutable-default-argument trap (memo={}).
Code / implementation expected: Optional β a short Python function (memoized or tabulated Fibonacci) is the usual illustration; the explanation matters more than the code.