Pocket Gems interview question

Algorithm