Amazon Interview Question for Software Engineer / Developers


Country: United States




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

I am assuming you are looking for the *least* common ancestor.

2. binary search tree

public static BSTNode lca(BSTNode root, BSTNode n1, BSTNode n2) {
    if(root == null || n1 == null || n2 == null) return null;
    if(n1 == root) return n1;
    if(n2 == root) return n2;
    if(n1.value < root.value && n2.value < root.value) {
        return lca(root.left, n1, n2);
    } else if (n1.value > root.value && n2.value > root.value) {
        return lca(root.right, n1, n2);
    }
    return root;
}

- vijay January 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Looks a good method for bst, it should work.

- Abhishek January 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Abhishek: yeah, I am not sure why the person who down-voted it didn't give a reason.

- vijay January 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

one minor fix is to check if n1 is smaller than n2.

- kang January 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

one minor fix is to check if n1 is smaller than n2.

- kang January 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

one minor fix is to check if n1 is smaller than n2.

- kang January 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

one minor fix is to check if n1 is smaller than n2.

- kang January 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

this code doesn't work if n1<root and n2>root..
if(n1.value<root.value && n2.value<root.value)
return root;

- Girish January 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Girish:
is the case you have mentioned taken care of by the last return statement "return root".

- kp January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

n1.value < root.value && n2.value < root.value .. one check would suffice.. rest is ok..

- minorfix January 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the given code works only if elements of tree are distinct

- Cairn January 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code only seems to be working for leaf nodes. In case one of the node to be checked is itself a parent of some other then this will give wrong result. Please correct me if wrong.

- Daman August 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Finally its returning root if none of the above condition is matched

return root;

- Shishir Chandra September 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

for the bst the time comp[lexity required is just the O(H) where H is the height of the bst :)

- geeeks January 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

//to search for common ancestor in a binary tree, the code would be

treeNodePtr findLCA(treeNodePtr root, int p, int q) {

// no root no LCA.
if(!root) {
return NULL;
}

// if either p or q is the root then root is LCA.
if(root==p || root==q) {
return root;
} else {
// get LCA of p and q in left subtree.
treeNodePtr l=findLCA(root->left , p , q);

// get LCA of p and q in right subtree.
treeNodePtr r=findLCA(root->right , p, q);

// if one of p or q is in leftsubtree and other is in right
// then root it the LCA.
if(l && r) {
return root;
}
// else if l is not null, l is LCA.
else if(l) {
return l;
} else {
return r;
}
}
}

- Anurag January 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice solution, with the only constraint that the algorithm will give wrong answer if p or q do not exist in the tree.

- abhishekatuw January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if p is q's ancestor?

- Anonymous October 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a fairly common question that is asked in many interviews. Refer to the Lowest Common Ancestor tutorial on TopCoder.

I found the last method(using the Euler Tour method to traverse the graph) more easier to understand than the other methods mentioned in the tutorial.

- coder January 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Would u plz care to explain how would that give the correct node

- anon January 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do a pre order traversal of tree. Scan the list of nodes obtained by pre order traversal. In this list a node which is last such node which was visited before these 2 nodes should be common ancestor.

This should work for for binary tree and binary search tree.

- Anonymous January 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Would u plz care to explain how would that give the correct node

- anon January 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this question was posted to this site just recently (< 5 days ago)

- eugene.yarovoi January 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For a BST this is very easy and Vijay has given the right algorithm.
For a binary tree you can calculate the list of nodes from the root to a particular node.
Do this for both the nodes. Compare their list of ancestors. The last element of the longest matching prefix is the LCA. Very simple.
(nobrainer .co .cc)

- Anony February 10, 2012 | 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