Question d’entretien chez Google

How would you optimize this solution? If you were to replace this algorithm with another, what would it be?