Morgan Stanley Interview Question

The question of leetcode