Pregunta de entrevista de Google

Had one algorithm/coding question to solve per interview.