Bloomberg interview question

a medium leetcode question and a hard one