Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

As it is a binary search tree , find the least and largest of all m numbers and find their LCA .

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

You didnt handle the case if key is equal to lowest OR key is equal to highest. This should come after the AND you are doing and if it is true you MUST return P.

One more thing for this code to work you MUST first traverse the whole BST and make sure that the input given does exist in the tree. Of course you can also ask your interviewer if the input is guaranteed to be in the tree.

- Rayden January 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
3
of 5 vote

//pass root as p , lowest and highest values in the given numbers m.
// i am assuming that all the given m numbers are present in the tree

int least_c_a(node *p,int lowest,int highest)
{
if((p->key>=lowest)&&(p->key<=highest))
return p->key;

else if(p->key<lowest)
least_c_a(p->rchild,lowest,highest);

else if(p->key>lowest)
least_c_a(p->lchild,lowest,highest);
}

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

Yes.. this seems the correct logic.. thanks for share

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

*correction I think the last else if statement should be:

else if(p->key > highest)
least_c_a(p->lchild, lowest, highest);

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

*correction I think the last else if statement should be:

else if(p->key > highest)
least_c_a(p->lchild, lowest, highest);

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

i think do it is as:
in each recursive call
1.check that if T==null return no any lca ;)
2.loop from i=1 to m and check for all vaues if any value equal to T->info return T->info.
3.initialize r=0,l=0;
4.loop again from i=1 to m if any value is less than the T->info then l=1 else r=1
5.after loop check if both l==1 and r==1 return T->info
6. if (r&& l==0) return the recusrive call of func passing T->right
7else return the recursive call of func passing T->left

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

the complexity of the code will be O(mH) where H is the height of the bst.

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

int* findLeastAncestor(int *root, int small, int large){

while(curr!=NULL){
    int *curr=root;
    if (curr->val > large)
        curr=curr->left;
    else if (curr->val < small)
        curr=curr->right
    else 
        return curr->val;
}
return NULL;
}

- coder123 January 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//this is for two elements only;
#include<iostream>
#include<string.h>
using namespace std;
struct node
{
node *l,*r;int data;
}*root;
void show(node *);node *insert(node *,int );int n;
void search(node *,node *,int ,int);
void search(node *sr,node *sr1,int d,int e)
{
node *p;p=sr;node *p1;p1=sr1;
while(sr!=NULL && sr1!=NULL)
{
if(sr->data==d || sr1->data==e)
{
cout<<"found\n";cout<<endl<<p->data<<" "<<p1->data<<endl;break;
}
p=sr;p1=sr1;cout<<endl<<p->data<<" "<<p1->data<<endl;
if(p->data!=p1->data) break;
if(sr->data>d) sr=sr->l;
else sr=sr->r;
if(sr1->data>e) sr1=sr1->l;
else sr1=sr1->r;
}
sr=root;node *g;g=sr;cout<<endl<<p->data<<endl;
while(sr!=NULL)
{
if(sr->data==p->data) {cout<<"again found\n";cout<<endl<<g->data<<endl;return;}
g=sr;
if(sr->data>p->data) sr=sr->l;
else sr=sr->r;
}

}
node *insert(node *sr,int a)
{
if(sr==NULL)
{
sr=new node;sr->data=a;sr->l=sr->r=NULL;return sr;
}
else if(sr->data<a)
sr->r=insert(sr->r,a);
else if(sr->data>a)
sr->l=insert(sr->l,a);
else return sr;
}
void show(node *sr)
{
if(sr!=NULL)
{
show(sr->l);cout<<sr->data<<" ";show(sr->r);
}}
int main()
{
root=NULL;int num;cout<<"enter how many\n";cin>>n;
cout<<"enter which one\n";
for(int i=0;i<n;i++)
{
cin>>num;root=insert(root,num);
}
cout<<endl;show(root);cout<<endl;int x,y;cout<<"enter the 1st searched element\n";
cin>>x;cout<<"enter the 2nd searche element\n";cin>>y;search(root,root,x,y);
return 0;
}

- uddeshyakumar.kumar February 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//this is for two elements only;
#include<iostream>
#include<string.h>
using namespace std;
struct node
{
node *l,*r;int data;
}*root;
void show(node *);node *insert(node *,int );int n;
void search(node *,node *,int ,int);
void search(node *sr,node *sr1,int d,int e)
{
node *p;p=sr;node *p1;p1=sr1;
while(sr!=NULL && sr1!=NULL)
{
if(sr->data==d || sr1->data==e)
{
cout<<"found\n";cout<<endl<<p->data<<" "<<p1->data<<endl;break;
}
p=sr;p1=sr1;cout<<endl<<p->data<<" "<<p1->data<<endl;
if(p->data!=p1->data) break;
if(sr->data>d) sr=sr->l;
else sr=sr->r;
if(sr1->data>e) sr1=sr1->l;
else sr1=sr1->r;
}
sr=root;node *g;g=sr;cout<<endl<<p->data<<endl;
while(sr!=NULL)
{
if(sr->data==p->data) {cout<<"again found\n";cout<<endl<<g->data<<endl;return;}
g=sr;
if(sr->data>p->data) sr=sr->l;
else sr=sr->r;
}

}
node *insert(node *sr,int a)
{
if(sr==NULL)
{
sr=new node;sr->data=a;sr->l=sr->r=NULL;return sr;
}
else if(sr->data<a)
sr->r=insert(sr->r,a);
else if(sr->data>a)
sr->l=insert(sr->l,a);
else return sr;
}
void show(node *sr)
{
if(sr!=NULL)
{
show(sr->l);cout<<sr->data<<" ";show(sr->r);
}}
int main()
{
root=NULL;int num;cout<<"enter how many\n";cin>>n;
cout<<"enter which one\n";
for(int i=0;i<n;i++)
{
cin>>num;root=insert(root,num);
}
cout<<endl;show(root);cout<<endl;int x,y;cout<<"enter the 1st searched element\n";
cin>>x;cout<<"enter the 2nd searche element\n";cin>>y;search(root,root,x,y);
return 0;
}

- uddeshyakumar.kumar February 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//this is for two elements only;
#include<iostream>
#include<string.h>
using namespace std;
struct node
{
node *l,*r;int data;
}*root;
void show(node *);node *insert(node *,int );int n;
void search(node *,node *,int ,int);
void search(node *sr,node *sr1,int d,int e)
{
node *p;p=sr;node *p1;p1=sr1;
while(sr!=NULL && sr1!=NULL)
{
if(sr->data==d || sr1->data==e)
{
cout<<"found\n";cout<<endl<<p->data<<" "<<p1->data<<endl;break;
}
p=sr;p1=sr1;cout<<endl<<p->data<<" "<<p1->data<<endl;
if(p->data!=p1->data) break;
if(sr->data>d) sr=sr->l;
else sr=sr->r;
if(sr1->data>e) sr1=sr1->l;
else sr1=sr1->r;
}
sr=root;node *g;g=sr;cout<<endl<<p->data<<endl;
while(sr!=NULL)
{
if(sr->data==p->data) {cout<<"again found\n";cout<<endl<<g->data<<endl;return;}
g=sr;
if(sr->data>p->data) sr=sr->l;
else sr=sr->r;
}

}
node *insert(node *sr,int a)
{
if(sr==NULL)
{
sr=new node;sr->data=a;sr->l=sr->r=NULL;return sr;
}
else if(sr->data<a)
sr->r=insert(sr->r,a);
else if(sr->data>a)
sr->l=insert(sr->l,a);
else return sr;
}
void show(node *sr)
{
if(sr!=NULL)
{
show(sr->l);cout<<sr->data<<" ";show(sr->r);
}}
int main()
{
root=NULL;int num;cout<<"enter how many\n";cin>>n;
cout<<"enter which one\n";
for(int i=0;i<n;i++)
{
cin>>num;root=insert(root,num);
}
cout<<endl;show(root);cout<<endl;int x,y;cout<<"enter the 1st searched element\n";
cin>>x;cout<<"enter the 2nd searche element\n";cin>>y;search(root,root,x,y);
return 0;
}

- uddeshyakumar.kumar February 18, 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