Solution for question in Quora Hackathon 2014