Bloomberg Interview Question

The time complexity of the solution