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.
Provide a concrete implementation plan using monotone queue optimization to compute DP where cost(i,j) respects monotone properties and sliding-window minima are required. Include pseudocode, invariant checks, amortized runtime reasoning, and discuss numerical stability and edge-case handling applicable to SRE telemetry sliding-window aggregation.
Write a Python function longest_common_subsequence(a: str, b: str) -> str that returns one LCS string between a and b. Input lengths up to 1000. Implement DP bottom-up (O(mn) time), reconstruct the LCS by walking back through the table, and discuss how to reduce space if only length is needed. Suggest SRE use-cases such as comparing config file histories.
Implement the Traveling Salesman Problem (TSP) using bitmask DP for N <= 16 in Python or C++. Return the minimal Hamiltonian cycle cost given a complete cost matrix. Discuss memory/time trade-offs, pruning heuristics (branch-and-bound), and meet-in-the-middle techniques for improving constants. Explain how this technique relates to small-scale service probing or rollout path planning.
Given an R x C grid (R,C <= 2000) where some cells are blocked, implement Python function count_paths(grid) that returns number of ways to go from top-left to bottom-right moving only right or down, modulo 1e9+7. Discuss DP with rolling arrays for memory efficiency and optimizations for sparse obstacles that SREs might encounter in large-scale topology maps.
Write an algorithm to compute the Longest Palindromic Subsequence (LPS) length for a string s with |s| <= 2000 using dynamic programming, and reconstruct one subsequence. Explain the relation between LPS and LCS(s, reversed(s)) and discuss memory/time trade-offs and practical optimizations for SRE string analysis tasks.
Unlock Full Question Bank
Get access to all 40 Dynamic Programming interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.