Amazon Interview Question for Software Engineer / Developers


Country: Canada
Interview Type: Phone Interview




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

Just check it for BST ..

public boolean isBST() {
        return isBST(root, null, null);
    }

   private boolean isBST(Node x, Key min, Key max) {
        if (x == null) return true;
        if (min != null && x.key.compareTo(min) <= 0) return false;
        if (max != null && x.key.compareTo(max) >= 0) return false;
        return isBST(x.left, min, x.key) && isBST(x.right, x.key, max);
    }

- Ajeet July 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

WRONG

- SD July 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 votes

Junk code.. where r u setting min and max

- Someone July 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Obvious bug is that any node with INT_MIN or INT_MAX will give false.

- SomTing Wong July 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just look at line "return isBST(x.left, min, x.key) && isBST(x.right, x.key, max)", here i am setting min and max.

- Ajeet August 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It will work perfectly with INT_MIN or INT_MAX, try it at your end.

- Ajeet August 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@SD
What is wrong .. ?

- Ajeet August 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Or you could do a inorder traversal, if all the nodes are in ascending order, then it is sorted. Here prev is used to record the node visited previously.

private TreeNode prev = null;
public boolean isSorted(TreeNode head){
    if(head == null){
        return true;
    }
    if(!isSorted(head)){
        return false;
    }
    if(prev == null){
        prev = head;
    }else{
        if(head.val < prev.val){
            return false;
        }
        prev = head;
    }
    if(!isSorted(head)){
        return false;
    }
    return true;
    
}

- ravio July 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 vote

Use recursion.

Base case: Leaf is always sorted.
Recursive case: return true if: left subtree is sorted and right subtree is sorted and that root_of_left_subtree < root < root_of_rightsubtree.

- Murali Mohan July 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

incorrect, see this example:
4
2 6
1 5 3 7

- yu_stfx@hotmail.com July 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

From the meaning of a 'tree' being sorted, I could only that much!

- Murali Mohan July 31, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Do a Inorder traversal. maintain the prev number and check if current is greater than prev.

//object level variable: int prev = Integer.MIN_VALUE; before this call
	public boolean isSortedAsc(Node root){
		if(root == null)
			return true;
		if(!isSortedAsc(root.lchild))
			return false;
		if(root.value < prev)
			return false;
		prev = root.value;
		return isSortedAsc(root.rchild);
	}

- Phoenix July 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

The solution is that each node in BST needs to be in certain range [min, max]

Wikipedia has good solution for this:

public static boolean isBST(TreeNode node, int maxData, int minData) {
    if (node == null)
        return true;
 
    if (node.getData() >= maxData || node.getData() <= minData)
        return false;
 
    return (isBST(node.left, node.getData(), minData) && isBST(node.right, maxData, node.getData()));
}


The initial call to this function can be something like this:


if (isBST(root, Integer.MAX_VALUE, Integer.MIN_VALUE))
    System.out.println("This is a BST.");
else
    System.out.println("This is NOT a BST!");

Essentially we keep creating a valid range (starting from [ MIN_VALUE, MAX_VALUE]) and keep shrinking it down for each node as we go down recursively.

- GatorsRock July 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int max = Integer.MIN_VALUE;
    static boolean sorted = true;
    
    public static void inOrder(BTree t){
        if(t==null) return;
        inOrder(t.left);
        System.out.println(t.num);
        if(t.num<BTree.max){
            sorted = false;
            System.out.println("Out of order at "+t.num);
        }
        BTree.max = t.num;
        inOrder(t.right);
    }

- tesh July 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sassdf

- DSFSA August 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

do a InOrder Traversal and at every node check the value of previous node. If prev val is greater then this val it means BTree is not sorted.

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

In-order and then check current with next value. If current is greater then next then not sorted or else it is.

- dronzer709 September 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

EDIT: Corrected. Thank you ravio.

public class BSTCheck {

static class TreeNode<T> {
    public T val;
    public TreeNode<T> left;
    public TreeNode<T> right;

    public TreeNode() {

    }
    public TreeNode(T x) {
        val = x;
    }
}
    public static void main(String[] args) {
        TreeNode<Integer> root = new TreeNode<Integer>(4);
        root.left = new TreeNode<Integer>(2);
        root.left.left = new TreeNode<Integer>(1);
        root.left.right = new TreeNode<Integer>(3);
        root.right = new TreeNode<Integer>(6);
        root.right.left = new TreeNode<Integer>(5);
        root.right.right = new TreeNode<Integer>(7);
        boolean sorted = isSorted(root);
        System.out.println("Sorted[" + sorted + "]");
    }

    private static boolean isSorted(TreeNode<Integer> root) {
        boolean sorted = isLeftSorted(root);
        sorted &= isRightSorted(root);
        return sorted;
    }

    private static boolean isLeftSorted(TreeNode<Integer> root) {
        return root.left == null || root.left.val < root.val && isSorted(root.left);
    }

    private static boolean isRightSorted(TreeNode<Integer> root) {
        return root.right == null || root.right.val > root.val && isSorted(root.right);
    }
}

Recurse left and make sure left child is less than current node. If the left child is null the default case is to return true since any false will negate the expression as a whole. Do the same to the right children except verifying that the right child is greater than the current node. Same handling of the null right child; return true.

- Chris July 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Your code is not correct. Your code will return true in the following case.
TreeNode<Integer> root = new TreeNode<Integer>(4);
root.left = new TreeNode<Integer>(2);
root.left.left = new TreeNode<Integer>(1);
root.left.right = new TreeNode<Integer>(0);
root.right = new TreeNode<Integer>(6);
root.right.left = new TreeNode<Integer>(1);
root.right.right = new TreeNode<Integer>(7);
boolean sorted = isSorted(root);

- ravio July 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

WTF does tree is sorted mean?

- Anonymous July 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think, it means like a binary search tree.

- Aditya Varma July 31, 2014 | Flag


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