Bloomberg Interview Question

A slight variation of leetcode 252