Pregunta de entrevista de Google

Coding based problem and SQL question as well