Question d’entretien chez Amazon

Frequently seen coding question. LC medium difficulty