Pergunta de entrevista da empresa Windsurf

Leetcode style algorithm question. It was hard.