Pregunta de entrevista de Amazon

Leetcode medium-hard level question for the OA