Pregunta de entrevista de Amazon

Was asked to solve a leetcode question