Pergunta de entrevista da empresa Epic

A basic algorithm problem. Not hard but would be required to optimize its performance.