Domanda di colloquio di Toptal

Algorithm question, based on leetcode