Amazon Interview Question for Developer Program Engineers






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

use a array list & keep a identifier for null value
start with root now
lets say root (12) have two children left (8) right (30) left of (8) is (4) right (10) left of (4) is NULL right is (6)

Keep this in array

12 08 30 04 10 -1 -1 -1 06 -1 -1 -1 -1 -1 -1
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15

now start with root 12
children is 08-left 30-right
children of 08 is 04-left 10-right
children of 30 is NULL-left NULL-right
children of 04 is NULL-left 06-right
remaining is NULL

It will reconstruct same tree

- Anonymous February 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Another way is Preorder Traversal

- Rahul Dashore February 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

for the 1st approch, how you could write tree nodes into array like what you say, in other word, when you stop your scan on the tree. In your example, you used level-order. The issuse is how could you know -1 on the 15th position is the last one of the tree....



For the 2nd approach, preorder/inorder/postorder would have the same problem. You cannot tell whether a particular number is left or right or cousin's child. If you mark as NULL, how to stop your scan,i.e, how could you make sure there is no other valid nodes in a deeper level.

- XO February 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can store completely and reconstruct identically a bst, by storing it's pre order traversal.
To reconstruct the bst identical to original, read the file and after reading each key, insert into the bst using a simple binary search tree insert.

- souravghosh.btbg February 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@saurabh: its binary tree not binary search tree.
@XO: by using inorder and preorder definatly we can reconstruct original binary trre..... try doing that... u will get to know....!

- PKT February 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Store preorder and inorder in file and reconstruct back:
int pre[10] = {50,40,25,70,5,45,20,10,35,85};
int in[10] = {70,25,40,5,45,50,10,20,35,85};
int search_result(int d, int root_d)
{
	int i=0;
	for(; i<10; i++)
	{
		if(in[i] == d || in[i] == root_d)
			break;
	}
	if(in[i] == d)
		return 1;
	else
		return 0;
}
void func(node **root, int d)
{
	if(*root == NULL)
	{
		*root = (node*)malloc(sizeof(node));
		(*root)->data = d;
		(*root)->left = (*root)->right = NULL;
	}
	else if(search_result(d, (*root)->data) == 1)
		func(&(*root)->left, d);
	else
		func(&(*root)->right, d);
}
void construct_tree_from_pre_in_order(node**root)
{
	for(int i=0; i<10; i++)
		func(&(*root), pre[i]);
}

- Anonymous February 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Will this algo work if there are duplicates?

- Anonymus February 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Agreed...you can construct a tree back if u have both preorder and inordered traversals but not just with the level order traversal

- Anonymous February 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think you can store the nodes in an array just the way its done for heap
node : Index : i
node.left : Index : 2i
node.right: Index : 2i + 1
The tree can be reconstructed from it easily, although it would require more space

- k3g February 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, if the tree is just a left-going chain with n nodes then we would need 2^n locations to store the tree.

- Jam February 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

My solution is
1. Save any two orderings [Inorder, Preorder, Postorder] to file.
2. Use XML file mechanism to store the tree.

- Karthik February 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

store inorder traversal with parenthesis (((a),b,(c)),d,((e),f,(g))). here d is the root connected to b and f. which inturn are connected to a,c and e,f respectively

- Arpit March 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Use BFS. Each level of the tree is written as a new line.

- Anonymous February 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Take a array size of 2^n-1.
root will be first element.Make binary tree with the help of zeros.
now fill in the array accorijdg to parent and child's address
left child will be 2i and right will be 2i+1

- ashish February 27, 2011 | 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