Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

We just need to serialize the binary tree in such a way that when we are de-serializing, we can identify the elements in the right and left subtrees. Thus, we can serialize the pre-order traversal of the tree with a special character ('#' etc.) between the left and right subtree of each node. Also, to make things simple, we can insert another special character ('$' etc.) in place of NULL pointers.

Now, we can recursively deserialize, since for each tree, we can always identify the root, elements in the left subtree and elements in the right subtree.

- DeathEater August 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Example plz...

- Guest Tom August 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Do a preorder but include the null nodes.

- Anonymous August 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes that should do the trick, we need to include the nulls to make sure that the tree built is sort of full.

- mr August 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

since we have to create the tree by de-serializing the data, so we should serialize in a way that we put the tree in complete binary tree format and put some special character in place of null childs.

- mr August 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

preorder will work only for BST. Level order will do the job... (include null nodes)

- Anonymous August 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Preorder with null nodes will work for any binary tree.

- Anonymous August 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

My code can work:
public static ArrayList<String> Serialize(BinaryTreeNode head){
ArrayList<String> arrayTree=new ArrayList<String>();
serializeProcess(head,arrayTree);
return arrayTree;
}


public static void serializeProcess(BinaryTreeNode head,ArrayList<String> arrayTree){
if(head==null){
arrayTree.add("#");
return;
}
arrayTree.add(String.valueOf(head.value));
serializeProcess(head.leftchild, arrayTree);
serializeProcess(head.rightchild, arrayTree);
}


public static BinaryTreeNode Reconstruct(ArrayList<String> map){
Stack<BinaryTreeNode> holdNode=new Stack<BinaryTreeNode>();
HashMap<BinaryTreeNode,Boolean> definedLeft=new HashMap<BinaryTreeNode,Boolean>();
BinaryTreeNode result=new BinaryTreeNode(Integer.valueOf(map.get(0)));
holdNode.push(result);
for(int i=1;i!=map.size();i++){
BinaryTreeNode current=holdNode.pop();
if(!definedLeft.containsKey(current)){
definedLeft.put(current, true);
if(!map.get(i).equals("#")){
current.leftchild=new BinaryTreeNode(Integer.valueOf(map.get(i)));
holdNode.push(current);
holdNode.push(current.leftchild);
}
else{
holdNode.push(current);
}
}
else{
if(!map.get(i).equals("#")){
current.rightchild=new BinaryTreeNode(Integer.valueOf(map.get(i)));
holdNode.push(current.rightchild);
}
}
}

return result;
}

- Chengyun Zuo August 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We could serialize by creating a array of the tree leaving empty positions if no left or right child. And de-serialize it like i root 2i left child 2i+1 right child. Although it will waste space but will give correct result.

- Ankur Garg August 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The format should be (Left sub tree),(root),(right sub tree). And again each sub tree can be sun divided into (LSB),(ro),(RSB).

While constructing , we should construct the sub tree , with the matching '(' ')'.

- Yuvaraj August 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Inroder+level-order..is a valid option..

- saraf2206 August 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public ArrayList<Integer> serialize(Pair head)
	{
		ArrayList<Integer> al = new ArrayList<Integer>();
		if(head==null)
		{
			al.add(null);
			return al;
		}
		
		Stack<Pair> s = new Stack<Pair>();
		s.push(head);
		while(!s.empty())
		{
			Pair node = ( Pair) s.pop();
			if(node==null)
			{
				al.add(null);
			}
			else
			{
                                s.push(node.second);
				s.push(node.first);
				al.add(node.val);
			}
			
		}
		return al;
		
	}
	
	
	public Pair cons_tree(Iterator<Integer> al)
	{
		Pair p = new Pair(null, null , 0);
		
		Integer i = al.next();
		if(i == null)
		{
			p = null;
			return p;
		}
		p.val = i;
		p.first = cons_tree(al);
		p.second = cons_tree(al);
		return p;
		
		
	}

- Adi September 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do a BFS and send $ instead of NULLs.

- John_Dalton September 26, 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