KatInterview

Crawling Worm Simulation: Track Length After Each Step

Data Structure DesignAuto-graded · 27 test cases
MediumAuto-gradedData Structure Designsimulationdequehash-set

Last updated

Tests precise deque-plus-set simulation of a growing worm on a grid, including the exact ordering of tail removal and collision detection.

Crawling Worm Simulation

A worm crawls around a rectangular grid of height rows and width columns. Rows and columns are both zero-indexed; the top-left cell is (0, 0). The worm begins as a single segment sitting on (0, 0) with a length score of 0.

Pellets appear on the grid one at a time, in the order given by food. Only the earliest uneaten pellet is present at any moment. The moment the worm consumes it, the next pellet in the list materialises. Once all pellets have been consumed, no new ones appear.

You are handed the complete list of moves up front. Simulate the worm's journey and return, for every move, either the current length score or -1 if the worm has already died.

Movement rules

Each character in moves shifts the worm's head one cell:

CharacterDirection
Urow decreases by 1
Drow increases by 1
Lcolumn decreases by 1
Rcolumn increases by 1

All other segments slide forward: each one occupies the cell its predecessor just left.

For each move, apply the following steps in this exact order:

  1. Compute the destination cell for the head. If it falls outside the grid, the worm dies and the game ends.
  2. If the destination cell holds the current pellet, the worm eats it: the score increases by 1, the next pellet in the list (if any) becomes active, and the tail does not move (the worm grows by one segment).
  3. If no pellet was eaten, the tail vacates its cell and the worm keeps its current length.
  4. If the destination cell is occupied by any segment that is still on the grid after step 2 or step 3, the worm has collided with itself and the game ends.
  5. Otherwise the head enters the destination cell and the move succeeds.

Important consequences of rule ordering

  • Following your own tail is always legal on a non-eating move. Because the tail vacates its cell (step 3) before the collision check (step 4), the head may legally enter the cell the tail just left — even for a two-segment worm reversing direction.
  • Eating onto the tail's cell is fatal. On an eating move the tail does not vacate (step 2 skips step 3), so that cell remains occupied and step 4 kills the worm.
  • A pellet sitting beneath the worm's body is not consumed on arrival. Pellets are consumed only when the head moves onto their cell. A pellet that spawns under an existing body segment (including the head's own cell) simply waits there until the head steps onto it later.

Output

Return a list of exactly len(moves) integers:

  • Entry i equals the score after move i, if that move succeeded.
  • Entry i equals -1 if move i ends the game, or if the game had already ended before move i.

Constraints

  • 1 <= width <= 10^4
  • 1 <= height <= 10^4
  • 0 <= len(food) <= 10^4
  • Each food[i] = [r, c] satisfies 0 <= r < height and 0 <= c < width. Pellet positions may repeat.
  • 1 <= len(moves) <= 10^4
  • Every character in moves is one of U, D, L, R.
  • The score never exceeds len(food), so every value in the output fits in a 32-bit signed integer.

Examples

Example 1

Input: worm_scores(4, 3, [[0, 2], [1, 2], [1, 1]], 'RRDLUL')

Output: [0, 1, 2, 3, 3, 3]

Example 2

Input: worm_scores(3, 3, [[0, 1], [0, 2], [1, 2], [1, 1]], 'RRDLUL')

Output: [1, 2, 3, 4, -1, -1]

Example 3

Input: worm_scores(2, 2, [[1, 1]], 'DRDU')

Output: [0, 1, -1, -1]

Hints

Hint 1

The tail removal and the collision check must happen in the right order. Ask yourself: on a non-eating move, should the tail cell be removed from your occupied set before or after you test whether the head's destination is free?

Hint 2

A deque handles the worm's body naturally — new head on the left, tail popped from the right, both in O(1). Pair it with a hash set of occupied cells so the self-collision check is also O(1) instead of a linear scan.

Hint 3

Avoid allocating a full height × width grid. With dimensions up to 10^4 each, that would be 10^8 cells. Store only the cells the worm actually occupies.