Pergunta de entrevista da empresa Capital One

How would you code the solution to this problem, and what is the run time?