Google interview question

A common problem from leetcode/CC150/any algorithm book.