Amazon Interview Question

about optimization the code they need optimal one