Impact Analytics interview question

Medium level dynamic programming question