Amazon Interview Question for Developer Program Engineers






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

Use an indicator node for the end of one level

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

here is the solution i proposes to the interviewer and he was happy at single shot...

take two ques Q1, Q2
//each time in any ques only the nodes belong to one level will be present
//hence print all the que nodes and print new line
enque root into Q1

while Q1!=empty
print node->data and enque the childs of all nodes in Q2

print("\n");
now process Q2,
while Q2!=empty
print node->data;
enque childs of each node in Q1.

print("\n");

if(Q1.empty() and Q2.empty())
break;//we have processes whole tree.

- solution... September 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good Solution , how did the interview go ? Can u publish other questions asked also

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

why do we need two Q's? Isn't one enough?

- GuestMB September 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good solution.Great approach.
try out with one queue it is solvable in that too.
But of course with similar complexities of both time O(n) and space O(n)

- Anonymous September 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes
Having a single queue will suffice. {{{ Push(Q) <- root while Q is not empty N <- Pop(Q) Print N Push(Q) <- children of N - AV October 31, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, my bad. Missed the newline part.
While pushing children in the queue, push a sentinel denoting a new line.

- AV October 31, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can traverse the tree and store the data and the level in a 2-D array.

Now accessing the array is a simple step

Space complexity : O(n2) [This can be compressed by using a 2-D list instead of predefined array].
Time Complexity : O(n)

- DashDash September 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I Solved the problem like this ,i used right sibling representation so that each node does not have store information about all its childs
1
\
2 -> 3 ->4
\ \
5->6 7->8
\
9
we are supposed to print
1
234
5678
9
I used q Queue[][] with Level information , traversed the complete tree and loaded the queue ,once done i print the nodes level wise , let me know if my approach is better or not ?

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

Interviewer first asked to come up with a Data structure , then i suggested the right Sibling approach tree structure . Other DS i can think of is Node where it stores all its childs in a linked list of nodes ... i feel first DS i used more better.

- KK September 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ibnipun10
Space Complexity is 2(N) right instead N2 , we are using a double dimensional array .

I solved this problem and wrote code for it and answered some general questions on binary search trees and trees , interview went for a hour , they said they will get back with in two days , does it mean i am rejected ?

- KK September 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I started implementing the Queue , but after i have written the 80 % program he said u can use Java Queue , so to complete the program i took around 20-25 mins time , I feel interviewer felt i am writing the code slow....

- k.krishnakarthik September 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is sample java programme for level order tree traversal.


package tree;

import java.util.List;

import sun.misc.Queue;


public class NTreeTraversal {

Queue traversalQueue = new Queue();

public void nTreeTraversal(NTreeNode root) throws InterruptedException{
if(root==null){
return;
}
System.out.println("Label Order tree traversal is -> ");
traversalQueue = new Queue();

traversalQueue.enqueue(root);
NTreeNode ptr = (NTreeNode) traversalQueue.dequeue();
while(ptr !=null){

System.out.println(ptr.getValue() + " \n");
addDirectChildInQueue(ptr);
ptr = (NTreeNode) traversalQueue.dequeue();

}
}

private void addDirectChildInQueue(NTreeNode node){
if(node == null){
return;
}
List<NTreeNode> directChildNodes = null;
NTreeNode ptr = node.getChild();
while(ptr !=null){
traversalQueue.enqueue(ptr);
ptr= ptr.getSibling();
}
}

public static void main(String args[]){

NTreeNode treeNode1 = new NTreeNode("1", null, null);
NTreeNode treeNode2 = new NTreeNode("2", null, null);
NTreeNode treeNode3 = new NTreeNode("3", null, null);

NTreeNode treeNode4 = new NTreeNode("4", null, null);
NTreeNode treeNode5 = new NTreeNode("5", null, null);
NTreeNode treeNode6 = new NTreeNode("6", null, null);
NTreeNode treeNode7 = new NTreeNode("7", null, null);
NTreeNode treeNode8 = new NTreeNode("8", null, null);

treeNode1.setChild(treeNode2);
treeNode2.setChild(treeNode5);
treeNode2.setSibling(treeNode3);
treeNode3.setChild(treeNode8);
treeNode3.setSibling(treeNode4);
treeNode5.setSibling(treeNode6);
treeNode6.setSibling(treeNode7);
NTreeTraversal ntreeTraversal = new NTreeTraversal();

try {
ntreeTraversal.nTreeTraversal(treeNode1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

- Mritunjay Kumar September 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="c++" line="1" title="CodeMonkey94755" class="run-this">#include <iostream>

using namespace std;

int main() {

queue<node> q;
int current_level = 0;
int next_level = 0;
q.push(root);
current_level=1;
while(!q.empty())
{
if(current_level == 0){
current_level = next_level;
next_level = 0;
cout<<"\n";
}
node temp = q.front();
for each child of temp{
q.push(child);
next_level++;
}
cout<<temp->data;
q.pop();
current_level--;
}



}
</pre><pre title="CodeMonkey94755" input="yes">
</pre>

- lingrong1988 September 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

counter = 1;
Add the root into Queue
while (!queue.empty())
{
n = queue.pop();
counter--;

Add all the childs of n into the queue
if (counter == 0) {
counter = queue.count();
print "empty tag";
}
}

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

If i am understanding it right..Can this be solved by breadth first search procedure where we push each node in to queue??

- Senthil September 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

yeah, one queue should do. Do a regular level order traversal with a single modification - when you have inserted all the children of a node, at the end insert a NULL to indicate level has to be changed.

- oshin September 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

{{
counter = 1;
Add the root into Queue
while (!queue.empty())
{
n = queue.pop();
counter--;

Add all the childs of n into the queue
if (counter == 0) {
counter = queue.count();
print "empty tag";
}
}
}}

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

What's a Developer Program Engineer?

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

you can do it without storing any auxiliary storage.

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

Hi, how about this code, just get ideas from "cracking the coding".

class Node
{
	int value;
	Node first;
	Node next;
	Node(...)
	{
		...
	}
}

public static int printEveryNode(Node root)
{
	ArrayList<LinkedList<Node>> array=new ArrayList<LinkedList<Node>>();
	LinkedList<Node> link=new LinkedList<Node>();
	link.add(root);
	int level=0;
	array.add(level,link);
	

	while(true)
	{
		link=new LinkedList<Node>();
		for(int i=0;i<array.get(level).size();i++)
		{	
			Node n=array.get(level).get(i).first;	
			if(n)
			{
				print(n.value);
				link.add(n);
				while(n.next)
				{	
					Node m=n.next;
					link.add(m);
					print(m.value);
				}
			}
			
		}
		level++;
		array.add(level, link);
		print("*********************************");
		
	}
	
}

- sheyuwang March 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

hey, just miss break conditions, welcome any critiques.

class Node
{
	int value;
	Node first;
	Node next;
	Node(...)
	{
		...
	}
}

public static int printEveryNode(Node root)
{
	ArrayList<LinkedList<Node>> array=new ArrayList<LinkedList<Node>>();
	LinkedList<Node> link=new LinkedList<Node>();
	link.add(root);
	int level=0;
	array.add(level,link);
	

	while(true)
	{
		link=new LinkedList<Node>();
		for(int i=0;i<array.get(level).size();i++)
		{	
			Node n=array.get(level).get(i).first;	
			if(n)
			{
				print(n.value);
				link.add(n);
				while(n.next)
				{	
					Node m=n.next;
					link.add(m);
					print(m.value);
				}
			}
			
		}
		if(link.size()>0)
		{
			array.add(level, link);	
			print("*********************************");
			level++;			
		}
		else
		{
			break;
		}
		
	}
	
}

- sheyuwang March 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can check my implementation here:

leniel.net/2011/11/nary-tree-graph-traversal-level-by.html

- leniel November 06, 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