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.
When reconstructing an optimal path/sequence from DP results at production scale, compare three strategies: (A) storing parent pointers for every DP state, (B) recomputing subproblems during backtracking on demand, and (C) checkpointing parent pointers at intervals and recomputing between checkpoints. For each strategy provide time/space cost models, practical sweet spots, and advice for ML workloads with very large tables that don’t fit in memory.
Implement palindrome partitioning that returns the minimum number of cuts needed to partition a string into palindromes. Provide a DP solution in Python that precomputes palindrome substrings and then computes min-cuts. Example: 'aab' -> 1 ("aa|b"). Discuss reconstruction of partitions and space/time tradeoffs.
Count the number of distinct subsequences of string s that equal t using dynamic programming. Implement a Python solution that handles large counts by returning result modulo 10^9+7. Example: s='rabbbit', t='rabbit' -> expected 3. Explain the DP recurrence and edge cases.
List common pitfalls and best practices when implementing top-down memoization in production ML code (Python). Cover issues such as recursion depth, memory growth of caches, thread-safety in multi-worker jobs, deterministic behavior for caching, and strategies for cache eviction or serialization.
Implement lcs_string(a: str, b: str) -> str in Python to return one longest common subsequence string (not just length). Use bottom-up DP to compute lengths and then reconstruct the subsequence via backtracking. If multiple LCS exist return any one. Example: a='AGGTAB', b='GXTXAYB' -> 'GTAB'.
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.