Implement substring search and weighted sampling | Google Coding Question