## Amazon Interview Question

Country: United States

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

Very cute problem. Had to think about it for a few minutes.

HINT: Look to work through cells (finite), not paths (infinite).

Algorithm is as follows:
*Add all cells to a min heap
*Take lowest node
*Check if path from top left to bottom right not using any previously seen nodes exists; if it does, this is the min on that path, and otherwise this is never a min
*Add the mins to a stack and the last one on is our answer

Coding and proof of correctness left as an exercise.

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

To elaborate slightly, as people are still posting wrong answers:
Take the minimum node, M1, on graph G. This is minimal on *any* path containing it which goes from start to end. So we just need to check there is a path (BFS M1 from start, S, and BFS end, E, from M1).

Then we "cross off" this node and consider Graph G' = G \ {M1}, with minimum M2. If any path from S to E through M2 on G' exists, M2 is minimum on it. So again, run BFS for M2 from S, then BFS for E from M2. If path exists, M2 is also a minimum.

We repeat this until we get to only one remaining node (think about earlier stopping points for micro optimisations if you wish - big O complexity does not change).

Complexity is simple. We consider V nodes, and run BFS on G for each.

Readers should notice a similarity with Prim's algorithm.

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

Note if we only need the maximal minimum, not all possible minimums, this problem can be solved almost immediately with a Max heap: add back in each cell until there is a path from S to E (how do we check there is a path?). The last edge we needed is the minimal edge on this path, and no paths exist only using larger elements. Again, compare this to Prim's.

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

I'm a little bit confused... if it is a matrix for sure there is a path that contains the cell with the smallest weight. Do a zig-zag through the matrix and there you go... a path that contains all the cels... so, yes... it is a trick question.

So, maybe I do not understand the problem, but just to a damn for through the matrix and find the minimum weight and the path is the zig-zag one.

I think the problem refers to some kind of minimum sum, then yes...you have a nice problem.

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

Yes, but I can take the smallest element in the matrix, find the next min element adjacent to it and construct a path that this element would be the smallest on.

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

This is a problem for Dynamic Programming. Obvious give away is the ends of the path in the corners. To confirm there is a greedy substructure of the problem.

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

Typical graph issue, using BFS and remember the visit state can solve it.
But you should clarify one issue at first, each node can only be access once or not.

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.

### 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.