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.

EasyTechnical
94 practiced

A robot starts at the top-left of an m by n grid and can only move right or down. Count the distinct paths to the bottom-right in O(mn) time, then reduce the extra space to O(n). Is there a closed-form answer, and why might you still prefer the DP?

EasyTechnical
92 practiced

Given an integer array, find the contiguous subarray with the largest sum and return the sum together with its start and end indices. Explain the DP state and transition behind your O(n) solution, and what you do when every element is negative.

EasyTechnical
71 practiced

A row of houses holds nums[i] dollars each, and you cannot take from two adjacent houses. Return the maximum total you can take in O(n) time. Define the state, the transition and the base cases, and then reduce the space to O(1).

MediumTechnical
93 practiced

Given coin denominations and a target amount, with unlimited coins of each kind, return the fewest coins that make the amount, or -1 if it is impossible. Define the state, recurrence and initialization, give the time and space cost, and then show how to recover the actual coins used.

MediumTechnical
83 practiced

Given a string s and a dictionary of words, decide whether s can be split into a space-separated sequence of dictionary words. Walk through your state and recurrence, give the complexity, and say how it changes if the dictionary is very large or you must return every valid segmentation.

Unlock Full Question Bank

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

Sign in to Continue

Join thousands of developers preparing for their dream job.