Dynamic Programming Questions
Solving problems with overlapping subproblems and optimal substructure via memoization and tabulation. Covers recognizing DP-amenable problems, defining state and transitions, 1D/2D formulations, and space optimization. Widely regarded as the highest-difficulty and highest-discriminating coding-interview topic.
Implement the Levenshtein edit distance algorithm in Java to compute the minimum number of insertions, deletions, and substitutions required to convert string A to string B. Your function should return the distance and also produce a sequence of edit operations. Then explain how to compute only the distance in O(min(n,m)) space, and when that space optimization prevents you from reconstructing the operations without additional work.
Describe the rolling-array (space-optimization) technique that reduces a DP from 2D to 1D. As a backend developer, apply this to the 0-1 knapsack problem: given N items (weight, value) and capacity W, show how to convert the standard DP into an O(W) space solution while preserving correctness. Explain the iteration order you must use and why that order prevents reusing an item multiple times.
You have dp[i] = min_{0 <= j < i} { dp[j] + cost(j+1, i) } for i=1..N, and cost(a,b) satisfies the quadrangle inequality and the argmins are monotone. Explain and implement the divide-and-conquer DP optimization to compute dp in reduced time (e.g., O(N log N) or O(N log C) depending on cost). Provide a correct recursive template compute(l, r, optl, optr) and explain why monotonicity of opt is required and how you'd validate it on sample data.
Implement the Longest Increasing Subsequence (LIS) algorithm in Java that returns both the LIS length and one actual subsequence using the O(n log n) approach. Explain how to maintain predecessor pointers to reconstruct the sequence, and discuss the clarity vs performance trade-off compared to the O(n^2) DP approach when shipping in a backend codebase.
Design and implement an algorithm to determine winning positions for a two-player impartial game where players alternately remove stones from a pile using moves from a custom set (e.g., remove a in S={1,3,4}) and some special moves depend on pile size. Provide a Python solution that determines whether the first player can force a win for N up to 10^6. Discuss computing Grundy numbers, periodicity detection, and optimizations for large N.
Unlock Full Question Bank
Get access to all 30 Dynamic Programming interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.