Pergunta de entrevista da empresa Meta

It was a hard question on leetcode