Pregunta de entrevista de Google

Technical coding question with decent difficulty