Pergunta de entrevista da empresa Uber

A classic dfs problem. clear code and logic is expected