Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
2
of 4 vote

This can be done in two phases.
1. In phase 1, find out the location of the grey cell by scanning the 2D array and let it's location be (i,j).
2. In phase 2, find out the paths from (0,0) to (i,j) and then from (i,j) to (N-1,N-1) using DP. The black cells can be treated as 'blocked' sites/paths and we have known DP solutions to traverse from a cell (i1, j1) to (i2,j2) with some paths blocked in between them.

- Murali Mohan January 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you please explain phase 2 more?

- Guy January 10, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think Step #2 should be just DFS on all possible routes from (0,0) to (n-1,n-1) until we find a route that satisfies condition: a route that has all the grey cells. We don't know which grey cell should be visited first, and it is not necessarily closest grey cell.

- K January 10, 2014 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

The two paths might intersect with each other.

- kk January 10, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

We can use a modified version of A*, having the heuristic to take into account the manhattan distance to the grey cell if the cell has not gone there yet. in addition we need to know for each candidate the path, so that we do not pass on the same cell twice. Thr main problem here is the space complexity. How do we store the path for each candidate. It could generate a huge amount of data.

- Anonymous January 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If the matrix were instead a graph, and we start at one vertex, must to through a gray vertex, and then finish at another vertex such that no edge is traveled twice, then we want to find two edge-disjoint paths from the gray vertex, one ending at the start location and the other ending at the end location. This can be solved with a network flow, where each edge has capacity 1, a flow of 2 starts at the gray vertex, and two flows of 1 are directed from the start and end to the sink. So a path is found iff the maximum flow is 2.

So it remains to transform the matrix into a graph. To do this, for each cell c, make two vertices c_in and c_out; direct an edge from c_in to c_out. Then, for each neighbor n, direct edges from c_out to n_in. This way, each path of the matrix must go through c_in and c_out for each cell in the path.

Time complexity with Ford-Fulkerson is O(N*M), since BFS is O(N*M) and max flow is only 2.

- Anonymous January 11, 2014 | 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