Amazon Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
-1
of 1 vote

<pre lang="" line="1" title="CodeMonkey6195" class="run-this">// this must be a directed graph

// a global vector to record the visited Nodes
Vector visited;

vector<Node> DFSFind (Node me, Stack<Node> myAncestors) {
// add myself as my ancestor
myAncestors(me);
visited(me);

// init my descendents
Vector<Node> myDescendents;

for (Edges e : me.outEdges) {
if (myAncestors.contains(e.tail)) {
// this is a reverse edge -> including self-reverse
}
else if (myDescendents.contains(e.tail)) {
// this is a forward edgs
}
else if (visited.contains(e.tail)) {
// since this node is visited but neither my ancestor or my descendent -> a cross edge
}
else {
myDescendents UNION DFSFind(e.tail, myAncestors);
}
}

return myDescendents;
}
</pre><pre title="CodeMonkey6195" input="yes">Note that the "forward," "reverse," and the "cross edges" are defined on a Depth First Search. So different DFS will result in different set of these edges.
So, based on a DFS,
on a given a node and one of its outer edge:
1. if the edge connects to one of its ancestor -> a reverse edge (beware of the self-reverse)
2. if the edge connects to one of the "KNOWN" descendent -> a forward edge
3. if this edge connects to neither an ancestor or a descendent but a visited node -> a cross edge

This is my opinion....
Please correct me if there is any error.

Thanks.</pre>

- Anonymous May 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

"Note that the "forward," "reverse," and the "cross edges" are defined on a Depth First Search. "

Why they are pretty much defined for BFS as well..

- KM May 22, 2011 | 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