Question d’entretien chez J.P. Morgan

How to solve the coding problem.