Lucid Software interview question

Be sure to study graphs and DP (final round).