Interview Question for Software Engineer / Developers


Country: Canada




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

Do DFS with backtracking.

- Anonymous April 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Here is C# code that does the DFS using variable "visited" to keep track of previous nodes. Return value is a List of Lists, each of which contains a valid/non-cyclical route from point A to point B. Method is instance method of origination node, taking in destination node.

public List<List<TreeNode>> GetPathToNode(TreeNode destination, List<TreeNode> visited)
    {
        List<TreeNode> currentPath = new List<TreeNode>(visited) { this };

        if (this == destination)
        {
            return new List<List<TreeNode>>() { currentPath };
        }

        List<List<TreeNode>> retVal = null;
        if (ChildrenNodes != null)
        {
            foreach (TreeNode child in ChildrenNodes)
            {
                if (!currentPath.Contains(child))
                {
                    List<List<TreeNode>> childPaths = child.GetPathToNode(destination, currentPath);
                    if (childPaths != null)
                    {
                        if (retVal == null)
                        {
                            retVal = new List<List<TreeNode>>();
                        }
                        retVal.AddRange(childPaths);
                    }
                }
            }
        }
        return retVal;
    }

- johny418 April 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That is way, way, way shorter than my implementation. I obviously did not know what I was doing.

- static416 April 10, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

My solution was:
1. Use Dijkstra's algorithm to find the shortest route and store that route in a map
2. For each route found, recursively remove one link in that route from the graph, and find the next shortest route and add that to the map.

With the dataset provided, this implementation works well. However, finding "all possible routes" in a graph is a bad idea, as it becomes very computationally expensive as the number of nodes and interconnectivity increase.

But now that I think about it, DFS with backtracking may have been conceptually simpler. Though I'm not sure it would have been faster.

- static416 April 06, 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