Bloomberg Interview Question

a medium leetcode question and a hard one