Pregunta de entrevista de Amazon

Mostly coding question of medium difficulty