Question d’entretien chez Google

how to solve the problem and time space complexity