IBM interview question

An average difficulty python algorithm question