Pergunta de entrevista da empresa Google

Problems similar in LeetCode. Might have some follow-up.