XYZ interview question

ballon burst problem from leetcode