Zynga interview question

Variation of Leetcode question 621