Microsoft Interview Question

A medium difficulty algorithm problem