Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
7
of 9 vote

Use Dijkstras algorithm

- dini June 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Hunter
bfs does not give u d shortest path if weights are associted with each edge

consider this graph

1--->2
 15

1--->3
   2
3-->2
   1

now min distance from 1->2 is 3
but bfs will give 15 as ans

- guneesh June 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can we use Floyd-Warshall algorithm for adjacency matrix (as number of nodes is <= 1000)? I saw this problem in Top Coder in Dynamic Programming algorithm tutorial section. Using Floyd-Warshall algorithm gives the O(N^3) time complexity.

- cloudburst August 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//basic algorithm.
	for  i <-- 0 to N-1
		d[i] = infinity
	for i <-- 0 to N-1
		for j<-- 0 to i
			d[i]=min{d[i], d[j] + w[i,j]};
// get d[N-1] if it is infinity, no path exists.

- Anonymous July 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

It depends on whether you want an efficient solution that works most of the time or a solution that's less efficient but works all of the time. Dijkstra's algorithm is a "greedy algorithm" and thus doesn't work 100% of the time.

I'm thinking something like a recursive solution, where at each vertex you traverse to each of the immediate neighbors, returning the shortest path at each level. Of course, you have to keep track of the visited vertices so as to not get into any loops.

- vogelmanj June 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hope you didnt understand the Greedy algorithm concept.
The solution which you suggested is itself a Greedy-Dijkstra algorithm.

- SantiagoYemmiganur June 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No it isn't. My explanation for my recursive solution wasn't very good, so I'll provide code later to show you what I mean.

- vogelmanj June 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here is an excerpt from wikipedia's entry on "greedy algorithms":

Greedy algorithms mostly (but not always) fail to find the globally optimal solution, because they usually do not operate exhaustively on all the data. They can make commitments to certain choices too early which prevent them from finding the best overall solution later. Nevertheless, they are useful because they are quick to think up and often give good approximations to the optimum.
If a greedy algorithm can be proven to yield the global optimum for a given problem class, it typically becomes the method of choice because it is faster than other optimisation methods like dynamic programming. Examples of such greedy algorithms are Kruskal's algorithm and Prim's algorithm for finding minimum spanning trees, Dijkstra's algorithm for finding single-source shortest paths, and the algorithm for finding optimum Huffman trees.

- vogelmanj June 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

My apologies. I originally didn't see it specified that this was for positive arc paths only. It seems Dijkstra's algorithm does work for positive arc paths.

- vogelmanj June 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Argument1:
Greedy algorithms dont work find for all the problems. It is true, that does not mean that in single-source-shortest-path-problem dijkstra's wont provide correct solution all the time.

Greedy algorithms wont work in some problems as in travelling sales man problem. There the Greedy-solution may lead to a city where all the paths are traversed and there is no untraversed way to go a next city. Thus for problem TSP it is not a 100 solution. BUT Greedy-Algorithm has a way to find single-source-shortest-path-problem correct all the time. Using dijkstra's.

Argument2:
As Problem is to find the shortest-path between two nodes where as Dijkstra's produces shortest-path tree, just run the Dijkstra's algorithm till you find the destination node. Report once you traverse the destination node instead of going for all nodes.

- SantiagoYemmiganur June 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This can be done using BFS..
1. In O(n + m) we can find if the vertices are connected. if not display ERROR
2. Else, BFS always give u the shortest path b/w two vetices

- Hunter June 20, 2012 | 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