Amazon interview question

Coding and gave me hints to upgrade the codes.