Uber interview question

time Complexity, How i would optimize from original solution.