Bloomberg interview question

Leetcode 3