Yahoo Interview Question

The same question on the leetcode.