Flexible Finance interview question

Leetcode medium question + follow-up