Microsoft interview question

How would you solve this python problem.