Pergunta de entrevista da empresa Meta

Is there any alternative solution to the coding problem? What about its complexity compared to yours?