Scott Logic interview question

A Complex leet code challenge