Virtasant interview question

Solve this problem from leet code: [leet code problem]