Domanda di colloquio di Amazon

Leetcode-style algorithm question. Was about medium difficulty.