Metron Labs interview question

About the given task, how you approach the code, optimisation.