Pergunta de entrevista da empresa Amazon

Leetcode medium question (no coding required)