Pregunta de entrevista de Amazon

Coding on Graph question which is medium level.