Bloomberg Interview Question

Leetcode 1396 and some follow up