Dynamic Programming Questions

Solving problems with overlapping subproblems and optimal substructure by memoization (top-down) and tabulation (bottom-up). Covers recognizing DP-amenable problems, defining state, transition and base cases, 1D and 2D sequence and grid formulations (knapsack and its variants, coin change, LIS, LCS, edit distance, sequence alignment, regular-expression and wildcard matching, counting-ways problems), interval DP (matrix chain, palindrome partitioning), space optimization with rolling arrays, solution reconstruction, longest paths in a DAG, game and expected-value DP, bitmask and tree DP, speeding up a DP (monotone queue, convex hull trick, divide-and-conquer optimization, bitsets, meet in the middle), and judging greedy versus DP. Ground covered elsewhere: probabilistic model inference, production caching and debugging practice, and general graph shortest-path algorithms.

EasyTechnical
75 practiced

Compute the nth Fibonacci number three ways: plain recursion, recursion with a cache, and an iterative bottom-up loop. For each, give the time and space complexity, and say roughly how many calls the plain version makes for n = 40. What goes wrong with each version when n is 100000 and the answer is wanted modulo 1e9+7?

MediumTechnical
93 practiced

Given two strings of length up to 2000, compute the length of their longest common subsequence. Define the table and recurrence, then explain how to cut memory from O(nm) to O(min(n,m)) and what that costs you.

EasyTechnical
75 practiced

In your own words, what is dynamic programming and what two properties must a problem have for it to apply? Give one sequence or string example and one numeric optimization example, and say how you would spot them in a new problem.

EasyTechnical
98 practiced

Explain the dynamic programming formulation of the 0/1 knapsack problem: what the state means, the transition, the base cases and the order you fill the table. Why does the recurrence give the optimal value, and why does the same idea not work as a greedy by value-per-weight?

MediumTechnical
90 practiced

Compute the minimum number of insertions, deletions and substitutions needed to turn one string into another (for example 'kitten' into 'sitting'). Define the state, base cases and recurrence, and give the time and space complexity, including how to reduce the space.

Unlock Full Question Bank

Get access to all 10 Dynamic Programming interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.