Pergunta de entrevista da empresa Google

Both question is a variant of leet code problem.