Google Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

You would not use Dijkstra's algorithm to solve the traveling salesman problem. Dijkstra's algorithm solves the single-source shortest path problem which gives the shortest path to each node from a singular starting node. Dijkstra's algorithm would be useful if it was necessary to go to each city and return back to the start after each trip.

You could, from the start node, determine the next shortest path to another node, i.e. the next closest node (taking care to not visit any nodes that had previously been visited), and continue this for each node until back to the start, but the simple greedy algorithm does not guarantee that you will get the overall shortest path from start to finish (and back to start).

- Evan February 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If so, how should we solve TSP?

- Guy February 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Finding an exact solution to the optimization TSP requires factorial, O(n!), time using a brute-force solution - enumerating all possible tours and determining their respective distances - and O(n^2 * 2^n) using a dynamic programming approach.

Generally, people use approximation algorithms to find an answer to an instance of a TSP that is "good enough." For specific versions of the TSP (such as the Euclidean TSP), there exists an approximation that is guaranteed to be within certain bounds of the actual minimum tour which can be done in polynomial time, in this case, the Polynomial Time Approximation Scheme (PTAS) for 2-Dimensional Euclidean TSP.

- Evan February 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do we use a combination of Dijkstras and Minimum Spanning Tree using Prims?, use Minimum Spanning Tree to go through all the nodes once, after that use Dijkstras to come back to the start node from the end node?, this might not be the best shortest path around, but an approximation of shortest path around.

- captain_haddock February 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Using an MST to approximate a solution to a TSP instance has been done on the metric TSP using the Christofides approximation algorithm and finds a solution that is within 3/2 of the optimum tour.

- Evan February 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can use Dijkstra for TSP graph where each edges only visit vertices once. For example, A->B->C->D->A. Or even if there's an additional edge from B->D but the weight is bigger than A->D.
I think it's not possible to use Dijkstra for any graph since Dijkstra complexity is E+VLogV, and TSP is NP.

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

In regular Dijkstra you choose the next edge as the one that's nearest to the source, and not already part of the tree. Well, instead of keeping track of used nodes by the whole tree, keep them by individual path (when you branch in 2nd direction from a node, you'll have to make a new path). When you check for where to extend your edge to, check only that it doesn't touch your current own path (grrr - there may be more than one way to get to that node, thus more than one path. No, we got here the shortest way possible, no?). Anyway, first you remove the source from all paths, so that it's ok to add it. When you add a node, see if it's the source, and if so see whether your path includes all nodes. If so, this should be a shortest path. If not, abandon this path. Not 100% sure this works though. And, it's not really the Dijkstra algorithm anymore, it's just similar - is this really the question?

- JeffD March 26, 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