Google Interview Question
Software Engineer / DevelopersCountry: United States
just move the one on the tail to the square on top of the head? (if the square on the on top of the head is filled, then stop)
Anon is right, its just moving tail to head.
lets say snake is of size 3 at HEAD->(2,2)->(3,2)->(4,2)->NULL,
then on MoveUp we,
1) update 4,2 => 1,2
2) (1,2) points to (2,2)
2) (3,2) ->NULL
so new list is
HEAD ->(1,2) -> (2,2)->(3,2)->NULL
There will be check to see if snake reaches 0th row, in that case he will die.
Also seperate thread will be adding new elements at the end of LL after N seconds.
to implement moves across all directions it needs to be double LL.
if it's really about the space, you can just store coordinates of the head and the rest of the snake store as a linked list containing the 2bit flag: UP/DOWN/LEFT/RIGHT
- 111 February 20, 2013then, when you move the head up, you need to calculate a new position of each "limb" of the snake starting from the head and going down to the tail. Then if it happens that the new position of the head coincides with that of one of the limbs, game is over..Otherwise, you just update 2bit flags in your linked list according to new positions