Pregunta de entrevista de Amazon

leetcode looking question but slightly harder