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.
Many backend DP cost models reduce to dp[i] = min_j (dp[j] + m_j * x_i + b_j). Explain and implement the Convex Hull Trick (CHT) to optimize queries over lines for monotonic x_i. Provide both an amortized O(1) stack/deque-based CHT for monotonic slopes and a Li Chao tree for arbitrary queries; discuss numeric stability, integer vs floating-point trade-offs, and how to integrate CHT into production code.
As a backend developer deciding which endpoints to pre-warm under a fixed budget K, endpoints i have warm cost c_i and expected saved latency v_i. Implement a Java function that selects endpoints to maximize total saved latency subject to total cost ≤ K (0/1 knapsack). Return the selected indices. Discuss time/space trade-offs and approximation/scale strategies for very large K or many endpoints.
Given a chain of matrices with dimensions p0, p1, ..., pn, implement a Python function that computes the minimum number of scalar multiplications needed to multiply the chain (matrix chain multiplication) and output the optimal parenthesization. Your solution should use DP and handle n up to ~200. Discuss the time/space complexity and mention any optimizations or heuristics you'd consider for larger n in a production system.
Given a tree representing microservices, placing a monitor on a service covers itself and direct neighbors. Implement an efficient tree DP in Java to compute the minimum number of monitors needed to cover every node. Use states like: placed, covered-without-placed, and uncovered; traverse with DFS and compute states bottom-up. Explain correctness and how to output the chosen monitor nodes.
Write a Python function that counts the number of ways to make amount M using given coin denominations where the order of coins does not matter. Return the result modulo 1,000,000,007. Constraints: number of denominations n <= 100, M <= 10,000. Explain the bottom-up DP approach using a 1D array and how you avoid counting the same combination multiple times.
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.