Apple interview question

Leetcode.