Google interview question

coding a question similar to leetcode