Domanda di colloquio di Amazon

Leetcode question of hard difficulty