Amazon Interview Question for Software Engineer in Tests






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

Boolean test(node n,int min,int max){
if (n.data<min ||n.data>max)
return false
else
return test(n.left,min,n.data)&&test(n.right,n.data,max)
}
call as test(root,-MAX_INT,MAX_INT)

- Sathya June 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Boolean test(node n,int min,int max){
if (n.data<min ||n.data>max)
return false
else
return test(n.left,min,n.data)&&test(n.right,n.data,max)
}
call as test(root,-MAX_INT,MAX_INT)

- Sathya June 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Boolean test(node n,int min,int max){
if (n.data<min ||n.data>max)
return false
else
return test(n.left,min,n.data)&&test(n.right,n.data,max)
}
call as test(root,-MAX_INT,MAX_INT)

- Sathya June 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

put a null-check in the beginning of your function and it will work absolutely fine.

Boolean test(node n,int min,int max){
if(node==NULL)
return;
if (n.data<min ||n.data>max)
return false
else
return test(n.left,min,n.data)&&test(n.right,n.data,max)
}
call as test(root,-MAX_INT,MAX_INT)

- Avinash July 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Make in-order traversation and check in prevValue <= curValue. Time: O(N). Space: O(1)

- m@}{ June 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

not neccessarly

- Anonymous June 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

why not necessarily? Inorder traversal should do!

- Myth June 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

conside a tree ( not a bst, 10 comes in right subtree of 15)
15
/ \
12 18
/ \ /
11 14 10
here if you do inorder
11 12 14 15 10 18
according to your logic its a bst

- truecool June 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

white spaces not preserved, typing again

15
         /    \
        12    18
       / \    /
      11 14  10

- Anonymous June 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

oops, m@}{ is right, i take my comments back. here 15 > 10 so inorder check fails, any other senario, where it can fail??

- Anonymous June 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

thats not a BST..he's making fun of you..inorder always gives sorted array for a BST,,lol

- fcuk June 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

thats not a BST..he's making fun of you..inorder always gives sorted array for a BST,,lol

- fcuk June 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int isBST(struct node* node) {
if (node==NULL) return(true);
// false if the max of the left is > than us

// (bug -- an earlier version had min/max backwards here)
if (node->left!=NULL && maxValue(node->left) > node->data)
return(false);

// false if the min of the right is <= than us
if (node->right!=NULL && minValue(node->right) <= node->data)
return(false);

// false if, recursively, the left or right is not a BST
if (!isBST(node->left) || !isBST(node->right))
return(false);

// passing all that, it's a BST
return(true);
}

- Vishakha June 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey38788" class="run-this"> public class ValidBST
{
public bool IsValidBst(BTreeNode node)
{
if ((node.LeftNode == null && node.RightNode == null) || node==null)
return true;

bool isValid = IsValidBst(node.LeftNode) && IsValidBst(node.RightNode);
if (node.RightNode != null)
isValid &= (int)node.Data < (int)node.RightNode.Data;
if (node.LeftNode != null)
isValid &= (int)node.Data >= (int)node.LeftNode.Data;
return isValid;
}
}</pre><pre title="CodeMonkey38788" input="yes">
</pre>

- Anonymous July 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Check out the recursive version

//pre-condition min - INT_MIN, max - INT_MAX

int checktree(struct node *root, int min, int max) {
        int ret = 0;
        if(root != NULL) {
                if(root->data >= min && root->data < max)
                        ret = 1;
                return ret && checktree(root->left,min,root->data + 1)  
                        && checktree(root->right,root->data, max);
        }
        else
                return 1;
}

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

Do an inorder traversal and check if the numbers are in increasing order.Time O(n) and space O(n)

- Anonymous December 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

efficient solution is just do inorder traversal of the BST, and it will give the the sorted list of nodes...if not giving sorted list...its not a BST..every node value should be greater than the every visited node value...

- borka July 11, 2014 | 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