Amazon Interview Question for Software Engineer / Developers


Country: United States




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

1, find the minimum of each binary search tree
2. call successor for each binary search tree

- Anonymous February 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

there's a way to do O(1) space tree traversal, which is called Morris Traversal. the idea is to add additional right link to a right leaf node's successor

- meow February 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This does seem a possible solution to me...Thanks

- Inquisitive March 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Here goes the logic:
1. Find first element in tree T1 as well as T2 while doing an inorder search (this is equivalent to finding smallest element in the tree.
- This can be starting from root, if left child is not null, leftmost child is the least node
- If no left child for root, root itself
2. Compare the two values, if not same return false else
3. Call inorder successor on both trees and find next smallest element
4. GO to step 2
5. If both the trees are exhausted (be careful here), return true else false (one is subset of other)

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

Could this possibly be solved using iterators? Get an iterator for each tree, then just keep checking is tree1.next().equals(tree2.next())

- Anthony February 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

GZ_Penny, where did you get the question from?

How would you traversal on a single tree without a O(h) space stack (implicity via recursion or explicity container) ??????

Bad question.

- S O U N D W A V E February 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Don't know what GC_PENNY is dreaming, but the "best" solution if parent pointers are not available seems to be (to me):

2 stacks, one for each tree.

Iteratively in order traverse each tree using independent stacks but check intermittently after each iteration in the obvious way for equality (and break if a no math condition is detected).

- S O U N D W A V E February 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

math=match*

- S O U N D W A V E February 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Stacks aren't O(1) space, so this doesn't work. it would work if there was no space limit, but doesn't work given the constraints of the question.

- Anonymous February 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correct.

- S O U N D W A V E February 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The Inorder of BSTs will be equal if both trees contain same elements .
We can check if two BSTs contains same elements by XORing all the elements in the tree with the other .

public boolean isBSTEqual(BinaryTree *root1,BinaryTree *root2){
        int xorOfBST1=getXOROFTree(root1);
        int xorOfBST2=getXOROFTree(root2);
        return xorOfBST1==xorOfBST2 ? true : false;   
    }
    
    public int getXOROFTree(BinaryTree *root){
        if(root==null) return 0;
        return getXOROFTree(root->left) ^ getXOROFTree(root->right) ^ root->data;
    }

- Vishal S Kumar February 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void isSame(node* root1, node* root2, bool& isSame)
{
if( root1==NULL && root2 ==NULL) {
isSame = true;
return;
}
if( (root1 == NULL && root2 != NULL) || (root1!=NULL && root2!=NULL) {
isSame = false;
return;
}
isSame(root1->left, root2->left, isSame);
if( root1->data != root2->data){
isSame = false;
return;
}
isSame( root1->right, root2->right, isSame);

}

- hellosachin0107 February 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(1) space is impossible. you must use either recursive method or a stack to traverse the tree.

- nanericwang March 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can we convert both into DLLs and compare them?

- Anonymous September 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote
- GK February 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 4 votes

It's not correct answer. You are assuming that the threes have the same structure whereas the threes can have different structure and the same in order traversal.
E.g

3
  /    \
 2     4

and

2
 \
  3
   \
     4

- thelineofcode February 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I got it, thank you :). So, in this situation we should rebuild one of the threes to make roots the same and then use the code above.

- GK February 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Also you are not taking O(n) space in the stack because of recursion.

- Avsa February 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I mean you are taking O(n) space in the stack frame.

- Avsa February 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oh yeah, what I meant in O(1) space is not using any buffer. Sorry for misleading.

- Guy February 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

public bool compareInOrder (Node root1, Node root2) {
    Node node1 = getInOrderNext (root1.left, root1);
    Node node2 = getInOrderNext (root2.left, root2);
    
    while ( node1 &&  node2) {
        if ( !node1.equals(node2) )
            return false;
        else {
            node1 = getInOrderNext (node1, null);
            node2 = getInOrderNext (node2, null);
        }   
    }
    if ( node1 || node2)
        return false;

    return true;
}

//the key thing is storing reference to parent in current.right, so you can go backward
public Node getInOrderNext (Node current, Node parent) {
    Node ret_value = null;
    while(current != null && ret_value == null) {
        if (parent != null) {
            parent.left = current.right;
            current.right = parent;
        }

        if (current.left != null) {
            parent = current;
            current = current.left;
        } else {
            ret_value = current;
            current = current.right;
            parent = null;
        }
    }
    return ret_value; 
}

- Anonymous February 24, 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