Pregunta de entrevista de Jane Street

Coding question, preferred written in python.