Pregunta de entrevista de Amazon

It was a standard leetcode coding question.