Amazon Interview Question for Software Engineer / Developers


Team: Kindle-Periodicals
Country: India
Interview Type: In-Person




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

Traverse the tree in-order and save each element in an array.
If the array is sorted, the binary tree is a BST

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

Solution is correct but terribly inefficient.
First doing a in-order traversal, which then gets followed by comparing every adjacent element to ensure it is sorted.

Sure asymptotically it still is O(n), but practically it will be very time consuming. Check my recursive solution below.

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

Bool FindBST(node *root)
{

if(!root)
return true;
static node *Prev=NULL;

FindBst(root->left);

if(prev!=NULL)

if(root->data<prev->data)
return false;

prev=root;

FindBst(root->right) ;

}

- NaiveCoder March 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A binary tree is a BST if each of the right and left nodes of the node is a BST. So this can be recursively as,

bool isBST(node *root)
{
if (root == NULL)
return true;

else
{
if(root->left-->data <= root-->data < root-->right-->data)
{
return( isBST(root-->left) && isBST(root-->right))
}
else
{
return false;
}
}
}

- Ananth March 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Method I - There is a necessity that there be a global element ( say a static element). This value shall point to the previous value in the inorder traversal. We need to check if the current value's data is greater than that of the previous data.

Method II - We can insert values into a stack as we go along with the inorder traversal. If the current value is greater than the peek of the stack..we check further till end. Otherwise return false.

Method III - We can put all the elements of the tree in an array ( in order traversal ofcourse) and then check if it is in an increasing order.

- Arvind March 19, 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