Pregunta de entrevista de Toptal

Algorithm question, based on leetcode