Amazon Interview Question for Software Engineer / Developers






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

This can be solved by using recursive approach. For each node, compare its value to left and right child. and "AND" all the possible outcomes. if it returns false for any single node then it is not BST, else BST.

- sur February 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well, the problem states that the tree could be very large. In that way, a recursion method would run out of the memory because the stack limitation. I think maybe breadth-based traverse is the answer. Of course, it requires that no single level of the tree will use up the memory.

- Anonymous March 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The only way I can think of is inorder traversal and check for sortedness. There is no way not to track the nodes in the tree since the last leaf node in the tree might be greater than any of its ancestors.

- Anonymous February 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The only way I can think of is inorder traversal and check for sortedness. There is no way not to track the nodes in the tree since the last leaf node in the tree might be greater than any of its ancestors.

- Anonymous February 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ bool IsBst(Tree *t) { return ISBstInternal(t,-infinity, +infinity); } bool IsBstInternal(Tree *t, int l, int r) { if(!t) return true; bool l = IsBstInternal(t->left, l, t->value); bool r = IsBstInternal(t->right, t->value, r); return l && r; } - Anonymous February 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Aargh. Fix formatting.

bool IsBst(Tree *t)
{
    return ISBstInternal(t,-infinity, +infinity);
}
    
bool IsBstInternal(Tree *t, int l, int r)
{
    if(!t) return true;
    
    bool l = IsBstInternal(t->left, l, t->value);
    bool r = IsBstInternal(t->right, t->value, r);
    
    return l && r; 
}

- Anonymous February 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

And of course, check for (duh!)

l <= t->value <= r.

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

bool verifyTree(Node root){
     if(root == null){
        return true;
     }
     Node l = root.getLeft();
     Node r = root.getRight();
     if(l.getValue() <= root.getValue() <= r.getValue()){
       return (verifyTree(l) && verifyTree(r));
     }
     else{
       return false;
     }
}

- XeqtR February 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

ur code doesnt catch this
consider tree with following
{ 3
/ \
2 5
/\
1 6
}

- Anonymous March 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What about the following approach:

static int max_so_far = -infinity;
public isBST(Node root) {
if (root==null) return true;
boolean leftBST = isBST(root.left);
if (root.value>max_so_far) {
max_so_far = root.value;
}
else {
return false;
}
return leftBST && isBST(root.right);
}

- soupnazi August 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I meant:

static int max_so_far = -infinity;
public isBST(Node root) {
   if (root==null) return true;
   boolean leftBST = isBST(root.left);
   if (root.value>max_so_far) {
      max_so_far = root.value;
   }
   else {
      return false;
   }
   return leftBST && isBST(root.right);
}

- Anonymous August 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isBST(Tree *rt,int val,int flag)
{
if (rt == NULL) return true;

if ( rt->left != NULL)
{
if (rt -> data <= (rt->left)->data) return false;
if(flag ==1 && (rt->left)->data < val) return false;
}

if ( rt->right != NULL)
{
if (rt -> data > (rt->right)->data) return false;
if(flag ==0 && (rt->right)->data >= val) return false;

}

- ashish December 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

**ignore previous code

bool isBST(Tree *rt,int val,int flag)
{
if (rt == NULL) return true;

if ( rt->left != NULL)
{
if (rt -> data <= (rt->left)->data) return false;
if(flag ==1 && (rt->left)->data < val) return false;
}

if ( rt->right != NULL)
{
if (rt -> data > (rt->right)->data) return false;
if(flag ==0 && (rt->right)->data >= val) return false;

}

return (isBST(rt->left,rt->data,0) && isBST(rt->right,rt->data,1));
}

- ashish December 03, 2010 | 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