Pergunta de entrevista da empresa Meta

It was leetcode based question. A slight modification