Pergunta de entrevista da empresa HSBC

asked to solve a puzzle. Solved it.