Pregunta de entrevista de Meta

Algorithmic questions, you should find solution with optimal time complexity.