Amazon interview question

Problem faced with past project.