Microsoft interview question

Based on dp and graphs