Question d’entretien chez Amazon

graphs question.time complexity of the approach,optimal solution