Yahoo interview question

The same question on the leetcode.