Apple interview question

Leetcode Hard question