Tally Solutions interview question

DSA easy to medium level question