Pergunta de entrevista da empresa Amazon

leetcode looking question but slightly harder