Applied Intuition interview question

Coding question with hard LC difficulty