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.
Explain the convex hull trick and implement an online variant (Li Chao tree or dynamic convex hull) to optimize DP transitions of the form dp[i] = min_j (m_j * x_i + b_j) where slopes m_j are arbitrary. Provide complexity guarantees and describe when Li Chao is preferable to deque-based CHT.
Palindrome Partitioning II: Given a string s (length up to 1000), compute the minimum cuts needed to partition s into palindromic substrings. Provide an O(n^2) DP solution that precomputes palindrome substrings and returns both the minimum cuts and one valid partition. Discuss edge cases and performance considerations.
Theory: Define 'overlapping subproblems' and 'optimal substructure'. For each definition provide a concrete algorithmic example where it holds (e.g., Fibonacci, shortest paths) and one where it doesn't. Explain why recognizing these properties is necessary before applying dynamic programming.
You are presented with a problem that seems greedy at first glance: choose intervals to maximize profit with overlapping penalties. Describe how you would test whether greedy suffices or DP is required. Provide a small counterexample where a natural greedy fails and outline the DP formulation that correctly addresses the problem.
Patience-sorting LIS O(n log n) algorithm and reconstruction: Describe and implement the O(n log n) algorithm for computing the length of the Longest Increasing Subsequence (the 'tails' method). Then show how to reconstruct one LIS using predecessor pointers and binary search. Provide complexity justification and sample code sketch.
Unlock Full Question Bank
Get access to all Dynamic Programming interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.