Amazon Interview Question for Applications Developers


Country: India




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

node LCA(node *root, node *n1, node * n2) {
if ( root == null ) return root;
if ( n1 == null ) return n2;
if ( n2 == null ) return n1;
if (( n1->value < root->valule ) && ( n2->value > root->value )) return root;
if (( n1->value > root->valule ) && ( n2->value < root->value )) return root;
if (( n1->value < root->valule ) && ( n2->value < root->value )) return LCA(root->left, n1,n2);
if (( n1->value > root->valule ) && ( n2->value > root->value )) return LCA(root->right, n1,n2);
}

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

would this work if we need to find the LCA of 250 and 220?   
    
       100  
       /      \
   50       200
  /  \         /   \
20 51        150 250
                       /
                      220

- BJ November 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

to clarify on the above comment, child of 200 are 150 and 250.
220 is the child of 250. Now would the above algorithm work if we need to find the LCA of 250,220?

- BJ November 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

node LCA(node *root, node *n1, node * n2) {
if ( root == null ) return root;
if ( n1 == null ) return n2;
if ( n2 == null ) return n1;
if (( n1->value <= root->value ) && ( n2->value >= root->value )) return root;
if (( n1->value >= root->value ) && ( n2->value <= root->value )) return root;
if (( n1->value < root->valule ) && ( n2->value < root->value )) return LCA(root->left, n1,n2);
if (( n1->value > root->valule ) && ( n2->value > root->value )) return LCA(root->right, n1,n2);
}

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

Start searching for both the nodes from the root.
The first node where the search path diverges is the LCA.
By divergence, i mean one value is less than the node and other is greater

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

Least common Ancestor is that node form which one of the node is on left side and other is right side.
so start searching the node from top s.t n >= first and n <= second.
For any intermediary node, the case would be n > both or n< both.
accordingly go to the right or left child.

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

It seems that there is duplicate question. Some someone has given a very good simplification about this problem. Can't remember whom it is. Basically it can be taken as two link lists. A --> TOP and B --> TOP, then this problem can be regards as if these two link list merge at some node.

- peter tang October 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int getHeight(Node n){
int h=0;
while(n!=null){
n = n.parent;
h++;
}
return h;
}
public LCA(Node p,Node q){

int h1 = getHeight(p);
int h2 = getHeight(q);
if(h1>h2){
swap(h1,h2);
swap(p,q);
}
int diff = h2-h1;
for(i=0;i<diff;i++){
q = q.parent;
}
while(p!=null & q!=null{
if(p==q){
return p;
} else{
p=p.parent;
q=q.parent;
}
}
}

- Naveen November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private Node LCA_Normal(Node root, int a, int b){
	if(root==null){
		return null;
	}
	if((root.data>a && root.data<b)||(root.data<a && root.data>b)) {
 		return root;
	}
	if(root.data>a && root.data>b) {
		return LCA_Normal(root.left,a,b);
	}
	if(root.data<a && root.data<b) {
		return LCA_Normal(root.right,a,b);
	}
	return root;
}

Complexity -- O(logn)

- DigitalFire November 12, 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