A Bag With a Fixed Number of Pockets

Easy data structure and algorithm practice puzzle.

A model needs a fixed-length vector, but the vocabulary of real text never stops growing. The hashing trick skips the vocabulary: hash each word straight to one of m slots and count. Here a word's hash is the sum of its character codes, and its slot is that sum modulo m. Given a list of words and m, return the m counts. Two different words can share a slot, and the model is simply left to live with it.

Solve it three ways: snap the jigsaw pieces together, fill in the blanks, or type it from scratch against the tests. This one is free to play, with no account.

Every line of the solution comes with a note explaining why it is there, and a step by step replay shows the code running. At level 3, where you type it yourself, you can ask for an AI mock interview on this problem that ends with a hiring manager's scorecard.

Then grow it into a real project with the Software Engineer button: this puzzle becomes the Sample Session Roll-up, a pipeline you can run and download. See every Software Engineer project.

More Machine Learning puzzles

All 28 Machine Learning practice problems

Browse all 676 puzzles