IBM interview question

Leetcode style question from easy to mid range.