FLEXE interview question

equivalent to a leetcode - medium.