The Home Depot Interview Question

How to optimize a part of the code.