Pergunta de entrevista da empresa Google

Nothing unexpected, but you need to optimize your answer in both time and space complexity.