Pregunta de entrevista de Amazon

Leetcode question with two parts