Pregunta de entrevista de Google

Both question is a variant of leet code problem.