Domanda di colloquio di Amazon

Coding on Graph question which is medium level.