Pregunta de entrevista de Amazon

medium difficulty problem question and follow up