Amazon Interview Question
Software Engineer in TestsCountry: United States
There seems to be a few problems with your code:
1. You need to return a true / false to indicate that the exit was found. If you return true, then then you don't need any more iteration / recursion.
2. This is a backtracking problem. Once false is returned (indicating that you hit a wall or you were recursing out of the bounds of the maze), you need to set that element in "visited" to false.
The code in its current form will not work.
We may think a little complicated here. Since we only have 2 doors, we only need to find the other door, no other options.
void findExit()
{
while(isMazeExit()==false)
{
if(forward()==false)
{
rightTurn();
if(forward()==false)
{
rightTurn();
rightTurn();
forward();
}
}
}
}
The code with a slight modification to rightTurn (returns the direction).
- Ariel February 02, 2012