solution to Google's coding problem