Amazon Interview Question for Software Engineer / Developers


Team: Intern
Country: United States
Interview Type: Phone Interview




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

store the two traversal (inorder,preorder) of left and right subtree in the disk


start building smaller(size suitable for memory load) tree starting from leaves nodes by the help of left and right traversal

if they are mirror image of each other then do same for rest of the tree (u can discard these smaller tree,since they are already mirror image)

and at any point of time if they are not mirror image then u can return false

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

If tree is huge, probably it will be containing huge satellite data(usually a structure). we can represent the tree by an array whose elements are the pointers to the data of the node, such that, the root resides at index 0 and for a given ith node, its left child stays at (2i+1) and right child stays at (2i+2).

initialize: i to 1 and j to 2
inside a while loop(running for (No. nodes-1)/2 times):
call compare( array[i], array[j] );
if j ==i+1 i=j+1 and j=2*i
else i++ and j--;
end while

if compare function fails: quit
otherwise if while loop runs for the required no. of times => mirror image true

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

we can use hash to do it

string hash(node* tree, bool left)
{
  if(tree == null)
    return goodhashfunction("0");

 if(left == true)
  return goodhashfunction(hash(tree->left) + hash(tree->right) + tree->data);
else
   return goodhashfunction(hash(tree->right) + hash(tree->left) + tree->data);
}

bool ismirror(node* tree)
{
   if(hash(tree->left, true) == hash(tree->right, false))
      return true;
   else 
      return false;
}

- superstar007 March 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How to load the tree since it's too huge to be loaded in memory?

- ferry March 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You do not need to have whole tree in memory - just one node at the time. Just load node when you enter fucntion. To be exact you need (DEPTH OF TREE) x (SIZE OF NODE) memory

- superstar007 March 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Another option

bool IsMirror(node* left, node* right)
{

//LOAD left AND right NODE FROM DISK
 
  if(left->data!=right->date)
    return false;
  if(left==null)
    return true;
  return IsMirror(left->right, right->left) && IsMirror(left->left, right->right);
}

bool IsMirror(node* n)
{
  IsMirror(n->left, n->right);
}

- superstar007 March 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

^^ brilliant....although i dint understand your has solution.

- Ananth March 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this would take a lot of memory due to recursive calls. we need to use iteration.

- viv March 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Run two DFS instances on the left and right subtrees and check if the two will see similar values. The left DFS will work the opposite of the right side DFS though: whenever you go left on the left subtree's DFS, you go right on the right subtree's DFS.

- a.akhavan.b March 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

store left, thn root, thn right (i.e inorder) traversal in one line...
Now store right, then root, then left (i.e reverse inorder) in second line of a file..
both lines shud b same.. if it is then it is reflected image..

- SHAN March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

we can do one more thing.. two pointers.. inorder pointer and reverse inorder pointer.. start travelling in inorder and in reverse inorder and check at every step whether both pointer matches.. if at any step, it doesn't thn it is not reflection..
It would be helpful if the input format of tree would b given..

- SHAN March 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

we are supposed to use iteration here since it wont take too much stack space since the tree is huge.
1) use BFS with a queue, but while enqueuing nodes enqueue

pseudo code
enqueue(root.left)
enqueue(root.right)
while(queue.size() > 0){
Node node1 = dequeue();
Node node2 = dequeue();

check if node1.data != node2.data return false;
else{
if( (node1.right == null && node2.left !=null) || (node1.right != null && node2.left ==null)
return false;
if(node1.right != null && node2.left!=null){
enqueue(node1.right);
enqueue(node2.left);
}
if(node1.left != null && node2.right!=null){
enqueue(node1.left);
enqueue(node2.right);
}
}
}

Also, each Node should have two int fields - leftId, rightId each store a unique id. we can use this id as the filename of a file in some default directory( the file stores the subtree below that node). if the leftId = -1 or (rightid = -1), we can access the child nodes as usual.
if leftid or rightid is some positive number, we should load that subtree into memory.
This approach would minimize the no. of disk reads. If we were to store each node on disk, that would require a lot of disk accesses which slow downs the execution of the program.

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

take two queue.. and do level wise travesal.
fetch in 1 queue one level and fetch 2nd queue its child nodes. compare queue1 nodes.doing hashing or compare it 1st node to last node and 2nd node to 2nd last node.... and so on.

now delete all node from queue now fetch the child of queue 2 in queue1 .now this time compare queue2 nodes..... and do same things.

- time May 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

If the tree is huge - the recursive inorder traversal will put heavy load on the system stack!
I would prefer BFS traversal using an Arraylist - the size of the arraylist will be close to (2^height of the tree).
At each iteration - you save a level in arrayList and then you compare with the other tree to check if its a mirror!

- RKD March 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It all looks good except that if the tree is more than size of memory then how to find the height of the tree?

- Anonymous March 16, 2012 | 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