Pergunta de entrevista da empresa Meta

Leetcode medium question, look them up!