Pergunta de entrevista da empresa UST

Asked to solve a leetcode question.