Pocket Gems interview question

Algorithm and Design question