Amazon Interview Question for Software Engineer in Tests


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

The code with a slight modification to rightTurn (returns the direction).

public void printSol(int k, int pozi, int pozj) {
		for (int i = 0; i < k; i++) {
			System.out.println(st[i][0] + ", " + st[i][1]);
		}
		System.out.println(pozi + ", " + pozj);
	}

	public boolean visited(int k, int i, int j) {
		for (int l = 0; l <= k; l++) {
			if (st[l][0] == i && st[l][1] == j) {
				return true;
			}
		}
		return false;
	}

	public void findExit(int[][] maze, int k, int i, int j) {
		if (isExit(maze, i, j)) {
			printSol(k, i, j);
			System.out.println("end sol");
			return;
		}

		st[k][0] = i;
		st[k][1] = j;

		for (int dir = 0; dir < 4; dir++) {
			while (!forward(maze, i, j, dir) && dir != -1) {
				dir = rightTurn(dir);
			}
			switch (dir) {
			case 0:
				if (!visited(k, i - 1, j)) {
					findExit(maze, k + 1, i - 1, j);
				}
				break;
			case 1:
				if (!visited(k, i, j + 1)) {
					findExit(maze, k + 1, i, j + 1);
				}
				break;
			case 2:
				if (!visited(k, i + 1, j)) {
					findExit(maze, k + 1, i + 1, j);
				}
				break;
			case 3:
				if (!visited(k, i, j - 1)) {
					findExit(maze, k + 1, i, j - 1);
				}
				break;
			default:
				return; // -1
			}
		}

	}

- Ariel February 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Anonymous February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This maze is linear. There is no need for a method called visited. If there are two doors, there is an entrance and the exit. If you enter the room, you just need to find the other door.

- Anonymous February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Bread first search? if at any time you need to turn left, just turn right 90 degree 3 times....

- jay February 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We may think a little complicated here. Since we only have 2 doors, we only need to find the other door, no other options.

- Joe February 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

void findExit()
{
   while(isMazeExit()==false)
   {
     if(forward()==false)
     {
      rightTurn();
      if(forward()==false)
      {
          rightTurn();
          rightTurn();
          forward();
       }
    }
  }
}

- Joe February 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But the doors may not be opposite in direction!!

- Amit February 17, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More