Amazon Interview Question for Software Engineer / Developers


Team: RCX
Country: United States
Interview Type: In-Person




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

public void Wrapper(Node root)
{
	PrintAllPaths(root, "");
}

public void PrintAllPaths(Node node, string pathSoFar)
{
	if(node==null)
	{
		Console.WriteLine(pathSoFar);
		return;
	}
	PrintAllPaths(node.left, pathSoFar + ", " node.data);
	PrintAllPaths(node.right, pathSoFar + ", " node.data);
}

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

What language is that? C#?

- Anonymous January 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What language is that? C#?

- Anonymous January 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes

- Anonymous January 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Small correction. Since node.data can be generic, we should use node.data.ToString()

- Anonymous January 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

While I am trying to write this code in C# it does not detect node.left, is there anything I had to include? Could you please help me.

Thanks

- Anonymous January 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if only the root exists? Then your solution will print out "root" twice.

Need to add an extra check to see if you are not already a leaf (no children).

Perhaps the correction is this:

if (node.left != null)
        PrintAllPaths(node.left, pathSoFar + ", " node.data);
if (node.right != null)
        PrintAllPaths(node.right, pathSoFar + ", " node.data);
if (node.right == null and node.left == null)  // this is a leaf, so just print it out
        Console.WriteLine(pathSoFar);

- PixelPerfect3 January 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

Here is another good solution,

puddleofriddles.blogspot.com/2012/01/print-all-paths-from-root-to-leaf-in.html

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

Can be done without recursion:

class Nodex
{
public:
	bool visited;
	int a;
	Nodex* parent;
	Nodex* left;
	Nodex* right;
};



Nodex* NodeHead; 
// set NodeHead to the root



void TraverseWitoutRecursion()
{
	Nodex* current;
	current = NodeHead;

	while(1)
	{
		if(current==NULL) break;

		if((current->left != NULL) && (current->visited==false))
			current = current->left;
		else 
			if((current->right != NULL) && (current->visited==false))
				current = current->right;
			else
				if(current->visited==false)
				{
					printf("node value = %d", current->a);
					current->visited = true;
				}
				else
					current = current->parent;
	}
}

- sergey.a.kabanov January 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

/*Given a binary tree, print out all of its root-to-leaf
paths, one per line. Uses a recursive helper to do the work.*/
void printPaths(struct node* node)
{
int path[1000];
printPathsRecur(node, path, 0);
}

/* Recursive helper function -- given a node, and an array containing
the path from the root node up to but not including this node,
print out all the root-leaf paths.*/
void printPathsRecur(struct node* node, int path[], int pathLen)
{
if (node==NULL)
return;

/* append this node to the path array */
path[pathLen] = node->data;
pathLen++;

/* it's a leaf, so print the path that led to here */
if (node->left==NULL && node->right==NULL)
{
printArray(path, pathLen);
}
else
{
/* otherwise try both subtrees */
printPathsRecur(node->left, path, pathLen);
printPathsRecur(node->right, path, pathLen);
}
}

/* UTILITY FUNCTIONS */
/* Utility that prints out an array on a line. */
void printArray(int ints[], int len)
{
int i;
for (i=0; i<len; i++)
{
printf("%d ", ints[i]);
}
printf("\n");
} http://www.geeksforgeeks.org/archives/6719

- superffeng January 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

inspired by inorder traversal written in c++

void find_path(struct node *ptr, std::string& path)
{
        if(ptr == NULL)
        return;

        if(ptr->l == NULL && ptr->r == NULL)
        {
                printf("path: %s %d\n", path.c_str(), ptr->n);
                return;
        }

        path.append(1, ' ');

        char buf[5]={0};
        sprintf(buf, "%d", ptr->n);
        path.append(buf);

        find_path(ptr->l, path);
        find_path(ptr->r, path);

        sprintf(buf,"%d", ptr->n);
        size_t n = path.rfind(buf);
        if(n != std::string::npos)
        path = path.substr(0, n);

        return;
}

- raja roy January 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void print_paths(node n1)
{

System.out.println("in print_paths");

if(n1==null)
return;
else
{
arr[jj++]=n1.data;
//System.out.println(sum2);
print_paths(n1.left);
print_paths(n1.right);
if(n1.left==null && n1.right==null)
{
for(int i=0;i<jj;i++)
System.out.print(arr[i]);

}
if(n1!=null)
jj--;


}

}

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

paths will be stored in array

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

This method below is not giving correct paths. Can someone please tell me the mistake.

public static void paths(Node node, LinkedList<Integer> list) {
if(node == null) return;
list.add(node.data);

if(node.left == null && node.right == null) {
print(list);
}
else {
paths(node.left, list);
paths(node.right, list);
}
}

public static void print(LinkedList<Integer> list) {
System.out.println("Contents of list: " + list);
}

e.g:
7
/
2
/ \
1 5

It prints:
7 2 1
7 2 1 5

- mihirk January 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if(node.left == null && node.right == null) {
print(list);
return;
}

- pinkiee March 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

def printAllPath(self, n="root", soFar=[]):
		if n=="root":
			n=self.root

		soFarCpy=list(soFar)
		soFarCpy.append(n.data)

		if n.left!=None:
			self.printAllPath(n.left, soFarCpy)
		if n.right!=None:
			self.printAllPath(n.right, soFarCpy)
		if n.left==None and n.right==None:
			print soFarCpy

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

void printpaths(root,int a[],int len){
}

- Anonymous March 26, 2016 | 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