Morgan Stanley interview question

Portfolio balances problem from Hackerrank - 1st round - medium