Google Interview Question

How to make it more performant