Pergunta de entrevista da empresa Meta

It was a BFS problem.