Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Essentially what you are doing here is for finding common ancestor on the binary search tree (BST) but not for binary tree in general. For regular binary tree you need to call checkValue() on both a and b on the given node (either left or right from the root), from there you can determine whether a and b are fall between root or not. Otherwise, you need to continue recursively on one of the child of the root.

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

Sorry ..... it was a binary search tree my mistake in writing the question

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

Here is my code...

bool IsPresent(Tree *root, int value)
{
if(root != NULL)
{
if(root->data == value)
return true;
else return (IsPresent(root->left, value) || IsPresent(root->right, value));
}
return false;
}

void FirstCommonAncestor(Tree *root, int lValue, int RValue)
{
if(root == NULL)
{
}
else
{
if(IsPresent(root->left, lValue) && IsPresent(root->right, RValue))
{
printf("Ancestor %d", root->data);


}
else
FirstCommonAncestor(root->left, lValue, RValue);
FirstCommonAncestor(root->right, lValue,RValue);


}
}

- DashDash April 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.


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