Pragmatic Play interview question

Task from leetcode (easy level)