Microsoft Interview Question

Medium problem in leetcode, usually a random one.