Pergunta de entrevista da empresa Toptal

Algorithm question, based on leetcode