Amazon Interview Question for Developer Program Engineers






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

I assume the program finds any shortest (or longest) path and then exits.

Define happy move as x++ or y++ and unhappy move as x-- and y--. An happy path consists of all happy moves and a happy path (if exists) is also shortest path. An unhappy path consists of happy moves and unhappy moves.

To find a shortest path, starting from S, on each node, the rat tries to make a happy move as a preference. If the rat is blocked anywhere, backtrack.

To find a longest path, starting from S, on each node, we tries to make an unhappy move as a preference. If the rat is blocked anywhere, backtrack.

This is a greedy algorithm and does not guarantee finding a shortest (or longest) path. Look forward to a better algorithm.

- jiangok October 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This problem can be reduced to finding the shortest path between two nodes in a graph if we treat each slot as a node and an edge exists when rat can move from one node to the other.
Then use some standard approach such as BFS to solve this problem

- anwing October 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since, S will always be (0,0) and F will always be (N,M). we can use A* algorithm.
Follow the connected components i.e. 1s but when two possible paths are given chose the one closest to F using A* heuristic

- chennavarri October 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

does A* algorithm always give the shortest path? I think it sometimes give the path that is not the shortest path.

- yuweihui2 January 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

is the diagonal movement allowed?

- Anonymous October 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(n*m) algorithm using BFS will give you the shortest path.

- Anonymous November 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@above
pls elaborate

- @above November 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

initially the mice is at (0,0) we push the unvisited neighbours of it into the queue(also the neighbour should not be blocked) and we note that they are at distance 1 unit from the source(0,0). Now we remove one element from the queue and push all its neighbours which havent been pushed into the queue earlier into the queue and note that they are at distance 2 units from source. We keep on doing this until we reach the final position or until the queue becomes empty. if the queue becomes empty it means that we cannot reach the destination at all.
This is the code for the rough idea.

int minDistance(const vector<vector<int> > &Input){
int m = Input.size();
int n = Input[0].size();
queue<int> x,y,L;
vector<vector<bool> > visit(m,vector<bool>(n,0));
x.push(0),y.push(0),L.push(0);
visit[0][0]=1;
static int dx[]={-1,0,1,0};
/*if diagonal movement is allowed u need to add 4 more values*/
static int dy[]={0,-1,0,1};
while(!x.empty()){
    int a=x.front(),b=y.front(),l=L.front();
    x.pop(),y.pop(),L.pop();
    if(a==m-1 && b==n-1){
          cout<<"Destionation is reachable"<<endl;
          return l;//l is the minimum distance.
    }
    for(int i=0;i<4;i++){
      int t1 = a+dx[i],t2=b+dy[i],m=l+1;
      if(t1>=0 && t1<m && t2>=0 && t2<n && !visit[t1][t2] && Input[t1][t2]){
          x.push(t1),y.push(t2),L.push(m),visit[t1][t2]=1;
           visit[t1][t2]=1;
      }
    }
}
return -1;//queue is empty, so path is not found
}

- python.c.madhav November 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@mannirox3: Question looks incomplete, You did not mention direction in which we can move? it could only left and right, or it could be left right top bottom or it can even be diagonal in all four direction

- Anonymous November 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in all 4 directions

- mannirox3 November 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess diagonal is also allowed. So there are 8 possible moves for the mouse. Otherwise the sample matrix has no solution only. Am I right?

- MouseHunter November 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don;t the question is correct. Every valid path from source to destination is the shortest path.

- Anonymous November 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No dude, its not a square matrix

- Anonymous January 30, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think in order to find the shortest path we will have to look at the available options .. Even in the single source shortest path algorithms any path selected initially to a node might not be the shortest but in the end we find the best path.. So all available paths will be traversed ..
This is wht i feel..

I am really luking fwd to see if someone really has a solution to this ..

- Sam December 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can't this be done recursively? i.e. assume A is the matrix

if (A[n-1][m] == 1) and (A[n][m-1] == 1), then F(N,M) = Min (F(n-1,m), F(n, m-1)) + 1.
if (A[n-1][m] == 0) then F(n,m) = f(n,m-1) + 1.
if (A[n][m-1] == 0) then F(n,m) = f(n-1,m) +1.
if ((A[n][m-1] == 0) && (A[n-1][m] == 0)) then there is no solution.

- ror December 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

FIND-PATH(x, y)

if (x,y outside maze) return false
if (x,y is goal) return true
if (x,y not open) return false
mark x,y as part of solution path
if (FIND-PATH(North of x,y) == true) return true
if (FIND-PATH(East of x,y) == true) return true
if (FIND-PATH(South of x,y) == true) return true
if (FIND-PATH(West of x,y) == true) return true
unmark x,y as part of solution path
return false

- gbaba January 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

BFS would be your best options. The maze of actually a graph where '1' are the vertices which have edges connecting other adjacent '1's and 0s do not have any edges connecting other vertices. Now all you have to do is run a BFS on the grid. You can keep a distance array where you can update the solution everytime or you can use a backtrack after finding the shortest path by maintaining a 'previous' array which stores the previous vertex you came from for every vertex. Standard problem .Also, no need to build a graph. There is a standard way to execute BFS in a grid. Just google

- Anonymous June 23, 2015 | Flag Reply


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