UBS interview question

A very tough python algorithm