IBM interview question

Leetcode problem of medium difficulty