Frage im Vorstellungsgespräch bei Amazon

Leetcode-style algorithm question. Was about medium difficulty.