Apple Interview Question

Leet code hard q with details