Metron Labs Interview Question

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