Pregunta de entrevista de Amazon

Leetcode medium question (no coding required)