IBM interview question

Solve a leetcode style question