Amazon Interview Question for Software Engineer / Developers






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

I can give idea to solve this problem
store the in-oder and post-order traversal output of binary tree in file
now with these two out we can reconstruct the same tree.

- solver November 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is how I solved it during interview,

TO STORE INTO FILE:
Use BFS and store the values whenever there is no child, store 'null'

TO RECONSTRUCT:
Read all nodes into array.
For 'n'th node left child is at (2n+1)th position and right child is at (2n+2)th position.
If those positions are 'null' then node doesn't have children.

- andy November 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice idea, however you will waste lot of space if the file, if binary tree is sparse.. image a tree with only right children..

- Narendra December 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I can give idea to solve this problem
store the in-oder and post-order traversal output of binary tree in file
now with these two output we can reconstruct the same tree.

- solver November 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use adjacency list of nodes in the tree
list size is n, number of nodes in tree

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

Storing inorder and postorder will destroy the structure of tree.

There was once problem asked in code jam 2009 on decision trees.
In that problem one decision tree was presented to user in form of file .
I think that would be better way to write a binary tree in file.

- nitin November 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

hey nitin can u plz explain how it will destroy the tree?

- Anonymous November 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

hey nitin can u plz explain how it will destroy the tree?

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

supppose tree is

2
1 3

And second tree is
1
2
3

having same preorder traversal.
So how you are going to reconstruct the tree.

- nitin November 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But the inorder traversal is different. This question is asked in google interview also and it's a property of binary tree that you can always uniquely construct it back with pre-order and in-order traversla

- dilip November 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if all the elements in the tree are same.?

in this case inorder and post order are same..

- sethuraj.32 November 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

we should save pointer to disk otherwise there is noway to solve problems sethuraj asked

- pcboy November 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can represent the tree as a(b(c,d),d(e,f)). This can be achieved by doing a Euler Tour.

To construct back, Just use a stack. Continue Pushing values (a,b,...) and popping back when you get a ')'

- logNguy December 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

pre-order and in-order will allow to construct the binary tree with the same structure

- Anonymous December 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in-order + preorder
or
in-order + postorder

- pansophism December 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about?
1. Write BFS preorder, write the parent's offset to each node's section and the fact that it's left/right child. This can be done quite easily with recursion. This has to be done to preserve the tree structure.

2. When reading, read each node in order, construct the node, stick it into a hashtable (key=offset, value=pointer of node).

3. When reading a child node, look up the hashtable with the parent's offset, get the node * of the parent, construct and node, and relink it with the parent.

- passerby December 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@zhaoyangster: Can you give details of how to use two x-order traversals together to save and reconstruct a binary tree?

- curious February 22, 2011 | 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