Amazon Interview Question


Country: India




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

node CA(Node root,int key[])
   {
     if(root==null)
     return null;
     int i ;
     for(i=0;i<key.length;i++)
     {
       if(!contain(root.left,key[i])
        break;
     }
     if(i==key.length)
     return CA(root.left,key);

     int j ;
     for(j=0;j<key.length;j++)
     {
       if(!contain(root.right,key[j])
        break;
     }
     if(j==key.length)
     return CA(root.right,key);

     return node;
}

boolean contain(Node root,int key)
{
   if(root==null)
    return false;
   if(root.data==key)
    return true;
   return (contain(root.left,key)||contain(root.right,key));
}

- getjar.com/todotasklist my android app December 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

is the complexity O(n^2)?

we can do the following to reduce time complexity.
1. inorder traversal the tree, and for key nodes, record their paths from root,
2. find the longest common parts from beginning of the paths.

- Anonymous February 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

First find the minimum and maximum element in the list of n nodes, and then apply standard LCA solution to min & max nodes.

- Anonymous December 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

suppose the tree is

1
    2     4
  3   5

5 is the right child of 2
now keys are 3 , 4 ,5

a/c to ur solution min is 3 and max is 5 so the CA should be 2
but CA should be 1

m i missing something

- getjar.com/todotasklist my android app December 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If it is BST then above solution works, if it is just binary tree (not BST) then this won't works.

- Anonymous December 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, its not BST. Its just a binary tree

- rams December 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Dont we need two nodes or therir key velues of which we have to find the common ancestor???

- Hector December 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Abran - Where did u have u r interview?? Bangalore??

- Anonymous December 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can we search in bottom-up fashion as the node that we hit first as ancestor of all the keys will indeed be the least common ancestor? The same may not be the case when we search top down.

- Anonymous December 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.List;

public class Lca {
	private static class Node {
		Node left;
		Node right;
	}

	private Node root;

	// Helper class
	private static class LcaSpec {
		Node lca;
		int numOfChildren;

		LcaSpec(Node lca, int numberOfChildren) {
			this.lca = lca;
			this.numOfChildren = numberOfChildren;
		}
	}

	// Gets the spec of its children.
	// Checks if one of its child is already an lca.
	// Checks if it is one of the nodes (uses naive reference equals) and
	// updates spec.
	//
	// Recursive and O(N x M)
	// N - size of tree
	// M - size of list of child nodes
	private LcaSpec getLca(Node node, List<Node> nodes) {
		if (node == null) {
			return null;
		}
		LcaSpec leftSpec = getLca(node.left, nodes);
		LcaSpec rightSpec = getLca(node.right, nodes);
		if (leftSpec != null && leftSpec.numOfChildren == nodes.size()) {
			return leftSpec;
		}
		if (rightSpec != null && rightSpec.numOfChildren == nodes.size()) {
			return rightSpec;
		}
		int numOfChildren = (leftSpec == null) ? 0 : leftSpec.numOfChildren;
		numOfChildren += (rightSpec == null) ? 0 : rightSpec.numOfChildren;
		if (nodes.contains(node)) {
			++numOfChildren;
		}

		return new LcaSpec(node, numOfChildren);
	}

	// Get LCA given list of child nodes.
	// Assumption is there exists such a node.
	public Node getLca(List<Node> nodes) {
		return getLca(root, nodes).lca;
	}
}

- kartikaditya December 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. For each node (in the multiple node list) store its path from root to itself say each path in an array. This can be done by pre - order search. Remember this not a BST.
2. Let A,B,C,.. M be such arrays. (count m).
3. Let n be the shortest of the above paths. you can also collect the arrays that yield the shortest paths.
for (i=0; i<n;i++)
{
if( i is not within the range of any one Array Say H)
{
then last element of H is the LCA;
break;
}

if(A[i] == B[i] == ... == M[i]);//great do nothing
else
{
A[i-1] is the LCA.
break;
}
}

- Doctor.Sai December 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

node *closest(node* root, mynode* p, mynode* q)
{
mynode *l, *r, *tmp;

if(root == NULL)
{
return(NULL);
}

if(root->left==p || root->right==p || root->left==q || root->right==q)

{
return(root);
}
else
{
l = closest(root->left, p, q);
r = closest(root->right, p, q);

if(l!=NULL && r!=NULL)
{
return(root);
}
else
{
tmp = (l!=NULL) ? l : r;
return(tmp);
}
}
}

- Raghav December 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey38495" class="run-this">mynode *closestAncestor(mynode* root, mynode* p, mynode* q)
{
mynode *l, *r, *tmp;

if(root == NULL)
{
return(NULL);
}

if(root->left==p || root->right==p || root->left==q || root->right==q)

{
return(root);
}
else
{
l = closestAncestor(root->left, p, q);
r = closestAncestor(root->right, p, q);

if(l!=NULL && r!=NULL)
{
return(root);
}
else
{
tmp = (l!=NULL) ? l : r;
return(tmp);
}
}
}



</pre><pre title="CodeMonkey38495" input="yes">
</pre>

- Anonymous December 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static BinaryNode leastCommonAncestor(BinaryNode root,List<Integer> integers){
		
		if(root == null) return null;
		for (Iterator<Integer> iterator = integers.iterator(); iterator.hasNext();) {
			Integer value = iterator.next();
			if(value == root.value)
				return root;
		}
		
		BinaryNode lnode = leastCommonAncestor(root.left, integers);
		BinaryNode rnode = leastCommonAncestor(root.right, integers);
		
		if(lnode == null && rnode == null) return null;
		if(lnode != null && rnode != null) return root;
		
		if(lnode != null) return lnode;
		else return rnode;
		
		
	}

- nmc December 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

a java implementation

- nmc December 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

As everybody has written code, The algorithm for finding the LCA in simple words is as follows:
Start from root (TOP: preorder traversal) . lets consider the 2 nodes given as input be n1 and n2. Search both the left and right subtree of the root. As long as you find both n1 and n2 being part of one of the subtree, drillAs we proceed from root towards the leafAs we proceed from root towards theAs we proceed from root towards the leaf leafAs we proceed from root towards the leafAs we proceed from root towards the leafAs we proceed from root towards the leaf down on that subtree ignoring the other. If you find a node such that n1 is in left subtree and n2 in right subtree or vice versa then it will be the LCA.

- Harish January 06, 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