Amazon Interview Question for SDE-2s


Country: India
Interview Type: In-Person




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

Tree is a directed acyclic graph. So, there should have only one path from root to any node in the tree. If this property is violated then the graph is not a tree and might have the potential chance for having a loop. We can easily check this by checking number of incoming edges to a node.

1) If a non-root node has more than one incoming edge then loop exists only if the edge is from a child to an ancestor.
2) If the root contains an incoming edge then loop surely exists.

If we have a tree like
a
/ \
b c
/ \
d e

Now assume there is an edge from d to b (d, a). This edge will create a loop according to (2). Now assume an edge from d to b: (d, b). Then there will be a loop according to (1). But if e have an edge from e to c : (e,c) then there exist no loop although the tree is not tree anymore as there are more than one path from root to c.

- zahidbuet106 December 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In this particular type of binary tree representation, a node can appear only thrice. As a child of parent and parent of two of its children. If the three appearance is violated, then binary tree has a loop. Consider the following binary tree. 8 appears 4 times and hence btree has loop.
10,8 8,9 12,11 12,20 10,12 20,15 20,25 8,5 5,3 5,7 3,8

- JDev December 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if one of the nodes has one(or none) child and a loop . So it will appear thrice(or twice) and escape your check

- Madhavi December 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If given list is:

(1, 2), (1, 3)
(2, 4), (2, 5)
(3, 6), (3, 7)

Given list can be converted into adjacency list:

1 --> 2, 3
2 --> 4, 5
3 --> 6, 7
4 -->
5 -->
6 -->
7 -->

On this list, we can run DFS / BFS to find we there is any revisit on a node. If a node is revisited then there is a loop else no loop.

- SK December 29, 2013 | 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