Pregunta de entrevista de Meta

Basic algorithmic question. Improve time and space complexity.