Sahl interview question

Hard/medium common problem from leetcode