Amazon Interview Question for Software Engineer / Developers






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

similar to how heapsort does: http://en.wikipedia.org/wiki/Heapsort
create an array to hold the tree.

left = root * 2
right = root * 2 + 1

use BFS to traversal the tree and place elements into the array. Then serialize the array.

- puzzle October 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But if the tree is very unbalanced, it will waste a lot of space. If a line eg, 2^(n-1) - n is wasted.

- JeffD September 19, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

pre-order is enough

- Anonymous December 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

preorder is enough in case of BST

but for any binary tree inorder with any preorde/postorder/level order traversal can be used to reconstruct a tree

- Anonymous July 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What do you mean by serialize a binary tree?

- Anonymous November 01, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Convert it in an array and then serialize it.

- JD November 02, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Say, you have:
----1
--2---3
-4-5---7
You need to be able to serialize it to, say, some string and then back.

- Anonymous November 03, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think we can use recursion:
make 2 functions - f1( node *n ) and f2( node* n )
f1 returns the tail of serialised tree rooted at node n and f2 returns the head of the serialised tree rooted at n.
All we need to do now is to modify the next ptrs of f1's return value and the current node.

node* f1( node* n ){
if( n==NULL )return n;
if( n->left == NULL && n->right == NULL )return n;
node* c1 = f1( n->left );
node* c2 = f2( n->right );
c1->right = n;
c1->left = NULL;
n->right = c2;
n->left = NULL;
}

Similarly u can write the code for f2() as well. In this I am using the right ptrs for linklist.(any of the two can be used)

- Nitesh November 05, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not post-order/pre-order/in-order?

- Saga Yao March 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just one of the ordering won't help to deserialize the exact tree. Using all three (or even two is wasteful: wastes both time and space).

- T March 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can use the combination of tree traversals to build the tree from the traversal array e.g we can use inorder and preorder to store the traversals of the tree and then later on restore the full tree. I think it is not possible to do the same with just a single traversal of any kind.

@T : but that is the only viable option. do you have any other way out to solve it? use the following mail to discuss further.

@ Nitesh : Your solution is wrong. Read my answer properly or you can mail me back for further discussion/advice at yao.saga@hotmail.com.

- Saga Yao August 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Loaing an array by using any of the tree traversal methods (in/pre/post/DFS,BFS) is easy. But, how to form a tree back from such an array?

- Helper December 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

do Bread First Search and store the data of each node in queue. BFS will give nodes level by level. That will help to re-generate the tree in exact form. When de-serializing, deque from the Queue and start creating BST tree.

- Krunal March 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution is perfect... O(n) time complexity, easily implementable with or without recursion, and no need for special insert function.

- tiputiger December 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Won't work if the tree is not balanced.

- shrivats May 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

here I am assuming input tree is BST

- Krunal March 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Given the inorder and postorder, you can rebuild the tree.

- Helper March 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Queue SerializeBT(TreeNode* root)
{
  Queue<Node*> q = new Queue<Node*>();
  if(!root)
    q.enqueue(root);
  else
    SerializeBT(root, q);
  return q;
}

void SerializeBT(TreeNode* node, Queue q)
{
  if(!node)
  {
    q.enqueue(node);
    return;
  }
  
  q.enqueue(node);
  SerializeBT(node->left, q);
  SerializeBT(node->right, q);
  return;
}

- JSDUDE May 22, 2013 | 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