Microsoft interview question

Challenging algorithm question.