Question d’entretien chez Uber

time Complexity, How i would optimize from original solution.