Chatham Financial interview question

Medium leetcode problem in coding assessments.