Pergunta de entrevista da empresa Clover

General algorithm question with efficiency accounted for