Amazon Interview Question


Country: United States




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

Probably find lca of n1 and n2. Now, find the path length from lca from n1 and n2 and sum it.

- Anonymous October 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I guess by distance you mean "length of the path from n1 to n2"...

so the idea is to do a special case on the diameter of the tree rooted at the given root, but only till nodes n1 and n2...

see here for more: geeksforgeeks.org/archives/5687

- JustCoding October 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1 way which comes in top of my head

a) find LCA
b) find the distance of node 1 and node 2 from LCA. Sum it up.

- AJ Gauravdeep October 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Assuming a binary tree, the solution should look like the following:
1. find the difference between the heights of the two nodes. (diff)
2. depending upon the difference in height, change the pointer to the node, such that, both the pointers are at the same depth.
3. While the pointers do not point at the same node, keep assigning the pointer to the parent of the current node. Also, keep counting the steps (d_factor).
4. Total Distance = ( 2 * d_factor) + diff

Here's the code snippet:
----
d1 = depthOf(n1);
d2 = depthOf(n2);
diff = d1 - d2;
if ( d1 > d2 )
{
for ( int i = 0 ; i < diff; i++ )
n1 = n1->parent;
}
else
{
for ( int i = 0 ; i < diff; i++ )
n2 = n2->parent;
}

dist_factor = 0;
while ( node1 != node2 )
{
node1 = node1->parent;
node2 = node2->parent;
dist_factor++;
}

totalDistance = (2 * dist_factor) + diff;
--

- Coder October 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Even when the tree is not BST we can find LCA by writting the inorder and pre-order traversal of the tree and then finding the first node in pre-order for which n1 and n2 are on different side.

then sum of distance for LCA to n1 and n2 is the ans

- Anonymous October 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

dist(n1,n2)= dist(root,n1)+ dist(root,n2) - 2* LCA(n1,n2)

- khunima April 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

1) Do an INorder traversal, store it in an array
2) count the elements between the n1 and n2.
3) Return the counter or print the elements

- Srikanta October 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Consider a non-binary tree.
Even given the assumption of binary tree, consider a full binary tree 3 levels deep (including root) where N1 was the root and N2 was the rightmost leaf.

- Ben October 29, 2012 | Flag


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