Amazon interview question

graphs problem could not able to solve