Domanda di colloquio di Uber

time Complexity, How i would optimize from original solution.