Amazon interview question

Algorithm leet code question (graphs)