Amazon Interview Question for SDE-2s


Country: India
Interview Type: In-Person




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

private static boolean isBstSame(BSTNode root1,BSTNode root2) {
		BSTNode temp1,temp2;
		Queue<BSTNode> q1 = new LinkedBlockingDeque<BSTNode>();
		Queue<BSTNode> q2 = new LinkedBlockingDeque<BSTNode>();
		if (root1 == null && root2==null)
			return true;
		q1.add(root1);
		q2.add(root2);
		while (!q1.isEmpty() && !q2.isEmpty()) {
			temp1 = q1.poll();
			temp2=q2.poll();
			
			if(temp1.getData()!=temp2.getData())
				return false;
			if (temp1.getLeft() != null)
				q1.add(temp1.getLeft());
			if (temp1.getRight() != null)
				q1.add(temp1.getRight());
			
			if (temp2.getLeft() != null)
				q2.add(temp2.getLeft());
			if (temp2.getRight() != null)
				q2.add(temp2.getRight());
		}
		return true;
	}

- Vir Pratap Uttam May 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
4
of 4 vote

There is the same problem in Leetcode called same tree.
Just do a level order traversal.

public class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        Queue<TreeNode> firstQueue = new LinkedList<TreeNode>();
        Queue<TreeNode> secondQueue = new LinkedList<TreeNode>();
        firstQueue.add(p);
        secondQueue.add(q);
        while(!firstQueue.isEmpty() && !secondQueue.isEmpty()){
            TreeNode first = firstQueue.poll();
            TreeNode second = secondQueue.poll();
            if(first == null && second == null){
                continue;
            }else
                if(first == null || second == null){
                    return false;
                }
            else{
                if(first.val != second.val){
                    return false;
                }
                firstQueue.add(first.left);
                firstQueue.add(first.right);
                secondQueue.add(second.left);
                secondQueue.add(second.right);
            }
        }
        return true;
    }
}

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

Here my solution using queue, the idea like iterative dfs.

bool isSameTree (TreeNode * a, TreeNode * b) {
	if (a == NULL && b == NULL)
		return true;
	if (a == NULL || b == NULL) 
		return false;

	queue < TreeNode* > qa;
	queue < TreeNode* > qb;
	qa.push(a);
	qb.push(b);
	while (qa.size() != 0) {
		
		TreeNode * first = qa.front(); qa.pop();
		TreeNode * second = qb.front(); qb.pop();
		if (first -> val != second -> val)
			return false;

		if (first -> left && !second -> left)
			return false;

		if (!first -> left && second -> left)
			return false;

		if (first -> left) {
			qa.push(first -> left);
			qb.push(second -> left);
		}

		if (first -> right && !second -> right)
			return false;

		if (!first -> right && second -> right)
			return false;

		if (first -> right) {
			qa.push(first -> right);
			qb.push(second -> right);
		}
	}
	return true;

}

- 40aDima September 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is BFS, not DFS.

- ravio September 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can we use queue?

- Dmitry September 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use queue and poll each data and compare . If at any times , the value is unequal , the trees are not the same .

- shukad333 September 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Same data and shape or same data?
For same shape and data the previous solutions are good enough.
But for different data i don't think so, Why?
because, All of you assume that the two tree have the same appearance order.
What if the two tree have the same data but they have different In-order, Pre-order and Pre-order, and yes that may happen since the question talks about Binary Trees not Binary Search Tree.

- mosab October 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In line 3 I meant "But for same data and different shape", and the solution is to do any kind of order, put the data in hashtable and go do any order on the other tree compare objects with hashtable false return false or keep until the hash table is empty.

- mosab October 21, 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