Apple interview question

Leet code hard q with details