Amazon Interview Question for Software Engineer / Developers






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

The question was with "... tree/graph" at the end. Why r u talking about tree and traversals?

Use Matrices. You can easily store tree and graph as matrix and then serialize and deserialize matrix.

- zdmytriv January 28, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It depends on space usage. Do you really want to use O(n^2) space to serialize a tree, which can be done in O(n) space?

But you are right. That should work.

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

what is serialization of a binary tree?

- sarthak July 23, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Writing the tree to some file in binary data is called 'serialization' and reading back the file to construct the same binary tree is 'deserialization'.

- Ravi August 02, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void Tree::Serialize(TNode* root, int a[], int& reclevel)
{
if( NULL == root)
{
a[reclevel++] = -1;
}
else
{
a[reclevel++] = root->data;
Serialize(root->left,a,reclevel);
Serialize(root->right,a,reclevel);
}
}

TNode* Tree::DeSerialize(int a[], int& reclevel)
{
if( -1 == a[reclevel])
{
reclevel++;
return NULL;
}
else
{
TNode* node = new TNode();
node->data = a[reclevel++];
node->left = DeSerialize(a,reclevel);
node->right = DeSerialize(a,reclevel);
return node;
}
}

- Ramu August 22, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi,
Doesn't it required to construct same binary tree in dserialization ?
Or I didn't understand Ramu's solution !!!
it will change entire structure of the tree !!!
someone help plz
katie

- katie September 26, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The answer is inorder and pre-order traversal combination gives an unique tree. Same works for inoder with postorder.

But with preorder and postorder u cant construct an unique tree.

Conclusion: Serialization - store inoder with pre/post oder traversal of the tree

Deserialisation - you can construct the tree back. Jus try it. :)

- Ram Kumar October 16, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

or we could use ()
a(b c(null e))

- lele August 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I understood the code, but was unable to understand the sentence - The answer is inorder and pre-order traversal combination gives an unique tree. Same works for inoder with postorder.

I see that you have done a preorder traversal and serialized the nodes.. and during unsearialization, I dont see anything related to inorder...

- neeta April 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Try appending 'L' or 'R' to the nodes as u do pre order travesal...
For example: tree like
6
/ \
3 9
/ / \
2 7 10

will be like: 6R3L2L9R7L10R

Then when u de-serialize, start inserting in the tree based on 'L' and 'R'

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

Any order of traversal is fine as long as the serialize and deserialize in the same order, AND you account for incomplete trees by representing an empty child something like '*' or 'x' in the serialized file. For example:

1
/ \
2 3
/ \ / \
4 5 6

Would be represented (using a depth first search) as:
123456x

For deserialization you must simply add the numbers in the same order as the depth first serialization. This is also assuming that we are only serializing integer values

- Jordan February 16, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

multiple trees can give same DFS. Think about it!

- ND April 20, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

hey if it means writing in file then we can go like this..
file will contain
1
23
4567

for empty child let put x.
so the code will be

int a=1,b=1;
s.push(root);
while(a!=0)
{
b=a;
a=0;

while(b>0)
{
p=s.pop();
if(p==X)
{
end loop;
}
if(p.left)
{
s.push(p.left);
a++;
}
if(p.right)
{
s.push(p.right);
a++;
}
Print p;
b--;
}
print Space;
}
\\ this code if for complete tree which doesnt miss any single child.
\\ Here if any child doesnot exist then assume it X.

- JiM.iiitm January 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Usually the point of serialization is to be able to _deserialize_ it later. Your algorithm looks like a one-way affair.

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

convert the tree into a heap -> serialize
convert the heap into a binary tree -> deserialize

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

Serialize means writing to a file. You can't come up with your own definitions!

Also, there must be a unique 1-1 mapping between the serialized and deserialized versions.


Read Ravi's post (dated August 2, 2007) about what it might mean to serialize and deserialize.

- Anonymous March 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote
- anil_Arya July 16, 2012 | 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