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.

MediumTechnical
71 practiced

You have a working top-down memoized solution, say for edit distance or LCS. Convert it step by step into a bottom-up solution: how do you decide the loop order, the base cases and the table dimensions?

HardTechnical
77 practiced

In a tree whose node weights can be negative, find the maximum sum of weights along any path between two nodes (the path may be a single node). Describe what each node returns to its parent versus what it records as the global best, and give the complexity.

MediumTechnical
81 practiced

Return an actual longest common subsequence of two strings, not just its length. Fill the table, walk back through it to build the string, and say what you do when several answers tie.

MediumTechnical
98 practiced

Houses form a binary tree, and you cannot take from two directly connected houses. Return the maximum amount you can take. Define what each node returns and how a parent combines its children.

MediumTechnical
68 practiced

Extend your edit-distance solution so that it also returns one sequence of edit operations that achieves the minimum, or shows the two strings aligned. How do you recover it, and what does that do to your space optimization?

Unlock Full Question Bank

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

Sign in to Continue

Join thousands of developers preparing for their dream job.