Medium data structure and algorithm practice puzzle.
A snake starts at row 0, column 0 on a board of the given width and height. Given the food positions, which appear one at a time in order, and a string of moves (U, D, L, R), return how many foods it eats, or -1 if it hits a wall or itself. Each move adds a new head. If it ate, the tail stays and the snake grows; otherwise the tail moves off first, so chasing your own tail is allowed. The body is a queue, new heads in and old tails out, and a hash set of occupied cells makes every collision check one lookup.
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.
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 Entity Ledger Balancer, a pipeline you can run and download. See every Software Engineer project.
Browse all 576 puzzles