Adobe Interview Question
Software Engineer / Developersif your snake is going zigzag and touching itself, how will you find out in which direction the tail should shorten? E.g.
v- tail
XXXXXX
X
X
XXXXX
XXXXX
XXXXX
XXXXXXXX <-- head
In the blob at the end..how do you know if the snake went
.---'
`---.
.---'
`------| <-- head
versus
/\/\|
|||||
|\/\/
'------| <-- head
Instead...how about have 0 for empty space, -1 for apple, -2 for wall, and each part of the snake has a value that counts up? e.g. the first part of the snake is 1, then 2, 3, 4,...50,51,...99,100. to erase then just check surrounding boxes for the box with the next highest digit and erase.
you will need to take into account the values getting too large, though, and will need to find an appropriate time to reset the values. (if you're storing ints in the array...you will be able to move ~2million spaces before this happens, though).
How about using a Linked List. Each node have its coordinate.
I implemented it some time back.
yes..this looks better
we can maintain a singly linked list with head and tail pointers containing coordinates in the nodes with new coordinates added at the head and deleted at the node. but first we need to create a 2d array of the map specifying special values indicators of food,wall,free,collision condition
1. food - add a new node at the head with food coordinates (length of the snake increases)
2. wall - hit wall, break out
3. free - add node at the head with free coordinates and hence updating the free coordinate status to collision and also delete the node at tail and hence updating the tail coordinate status to free ( length of the snake remains same)
4. collision - signifying that the current coordinate is in the cirrent list and hence collision of snake to itself. break out
so we start creating a list by adding a free coordinate at start into the linked list and wait for user next direction. we check the next coordinate from the map status and hence updata the list or map accordingly.
n X n Array.
- Ashok February 28, 2011Each box having either of the following:
1. 0 if it is empty and snake can move.
2. 1 if snake's body is in that box.
3. 2 if box is the snake food (using which snake grows)
4. 3 if there are any walls.
Need 2 objects to store the start and end location of the sanke.