Pergunta de entrevista da empresa Oracle

random leetcode question in medium-hard level.