Question d’entretien chez ByteDance

Is there a faster way to implement this solution?