Pergunta de entrevista da empresa Amazon

Leetcode question with two parts