Google interview question

it was similar to leetcode