Amazon Interview Question


Country: United States
Interview Type: Phone Interview




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

Find least common ancestor of two nodes, then you can find distance between them.

- Nitin Gupta February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

one small addition: before looking for LCA by using the standard algorithm(find a node with a value that is greater than first node value and less than 2nd node value) check if node1.left = node2 or node1.right = node2 or node2.left = node1 or node2.right = node1 and return 1 if it is true

- S.Abakumoff February 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Find LCA then use the below to find distance between two nodes

(distance from node 1 to root + distance from node 2 to root)-(2*(distance from root to LCA))

- dude March 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

With languages that pass parameters with references like JAVA, your "root" variable will be changed after execution. Other than this, I think it is correct.

- Zhi Qiu February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'm afarid you are not right. Java passes objects as references passed by value. Should you modify, say, root.val, it will modify it indeed. Changing root won't do that.

- denizko February 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes you are right, thanks!

- Chih.Chiu.19 February 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I agree with @nitingupta180 , once you find the least common ancestor of two nodes , you need to find the distance from the first node to LCA and from LCA to second node, then add them up.

- goutham467 February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But isn't this what he did?

- Chih.Chiu.19 February 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Isn't he assuming that both the values are on the same side of the root?

- AAK February 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can do a preorder traversal on the binary tree.
Time compexity O(N), space complexity, O(log(n))

int findDistance(const Node * root, int val1, int val2)
{
    // check if the tree is empty or not
    if(root == NULL)
    {
        std::cout<<"Empty tree" << std::endl;
        return -1;
    }

    // check if the two values are equal or not, if they are
    // then the distance between the two is obviously zero
    if(val1 == val2)
        return 0;

    // have a stack to hold the nodes for back traversal
    std::stack<Node*>  stack;

    // For simplifying our code, we will make val1 < val2
    if(val2 < val1)
    {
        val1 = val1 + val2;
        val2 = val1 - val2;
        val1 = val1 - val2;
    }

    // initial distance betwen nodes is -1
    int distance = -1;

    while(!stack.empty() || root != null)
    {
        // traverse to the left of the subtree at root
        if(root != null)
        {
            if(root->val == val1)
                distance = 0;

            // if value of this node = val2, then we are done
            // return distance calculated so far
            else if(root->val == val2)
                return distance;

            stack.push(root);
            root = root->left;

            // this node might be on the path between val and val2
            // increment distance
            ++distance;
        }
        // else go right
        else
        {
            root = stack.pop();

            // this node is not on the path between val and val2
            // decrement distance
            --distance;

            root = root->right;
        }
    }
    return distance;
}

Please let me know if I have missed any corner case or if the logic needs some change

- as09 February 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hey abhishek

What is use of this code to exchange the value of val1 and val2 ?
if(val2 < val1)
{
val1 = val1 + val2;
val2 = val1 - val2;
val1 = val1 - val2;
}

We can read these values as per our convince.

Another thing is that , i don't think so it will give required result.
It would be great if you explain with some example.

- urlukyfren February 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since both the given values are present in BST, just finding out depth(root, val1)+depth(root,val2) is enough right. This will give us the answer.
May be for validation sake we can find out if the given node values are actually present or not.

- Ashish19121 March 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it is correct ... only one missing thing I guess is +1 in the end.

return depth(root, val1) + depth(root,val2) +1

- Anonymous June 11, 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