Barclays interview question

One leetcode medium coding question.