Apple interview question

Algorithm design