Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

use depth first search or breadth first search. Complexity, time = O(E + V), space = O(V)

- as09 February 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't think you can do it easily using BFS. Only DFS will be applicable.

- famee February 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why couldn't you do it with BFS?

- eugene.yarovoi February 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 votes

While technically you could use either BFS or DFS, predominantly, DFS is used to find cycles in graphs.

DFS is more memory efficient than BFS as you can backtrack sooner, and it's also easier to implement. Once DFS finds a cycle, the stack will contain the nodes forming the cycle. The same is not true for BFS, so you need to do extra work if you want to print the cycle that was found.

Another characteristic to take into account is if the graph is directed or undirected. If you're dealing with a directed graph, then you have to not just remember if you have visited a node or not, but also how you got there. Otherwise you might think you have found a cycle but in reality all you have is two separate paths A->B, but that ~doesn't mean there is a path B->A. With a DFS you can mark nodes as you descend and unmark them as you backtrack.

- kbex07 February 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think back edge is just for directed graph not work well for undirected graph?

- Anonymous February 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

Check for a back edge in the DF traversal of graph.

- alex February 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

you may use union-find technique with path compression for finding the cycle. if any 2 nodes of current edge has same root then it will form a cycle

- Ajay February 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yes a back edge detection can easily prove that the graph is cyclic

- Ajay February 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use strongly connected components.

- Anonymous February 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

In non directed graph with N nodes, if there are more than N-1 edges then it must have cycles. So all we need to do is find the number of edges and if they are equal or more than number of nodes then a cycle exists.

- nick February 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That applies only for a connected graph. If a graph is not connected, it could have fewer than N-1 edges and still have cycles.

- eugene.yarovoi February 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Toplogoy sorting

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

I think this is the exact thing the interviewer was looking for

- pras July 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If we are dealing with undirected graphs there is a solution with O(V) complexity, we need to check that the graph is a forest, otherwise it will have cycle.

For a Directed Graph a simple DFS would reveal a cycle if we found a back node during the execution. The complexity would by O(V + E).

- pisaruk February 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Toplogical Sorting will be good solution.

- Ashish19121 March 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

When you want to find the cycles (for establishing a cycle base), the BFS tends to find much smaller cycles. The computation time tens to depend on the average cycke length, not on the speed of your spanning tree construction. That is why BFS is better.

- Jasper van Casteren September 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.


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