Microsoft interview question

A medium difficulty algorithm problem