Amazon Interview Question for Software Engineer / Developers






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

If I remember correctly, one algorithm is to find the deepest node of the tree say D1, then assuming that D1 is the root, find the deepest node again, say D2. D1 to D2 will be the longest path.

If your tree has both child/parent pointers, this should be O(n) time algorithm, where n is the number of nodes. (assuming the tree is connected).

- T March 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Test, Please ignore.

Next line empty

    4 spaces in beginning.
Next line 2 spaces
  
    4 spaces in beginning.


2 Empty lines above.

- T March 24, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

cant u use depth first search (dfs)

- sharad kumar.j March 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

cant u use depth first search (dfs)

- sharad kumar.j March 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

cant u use depth first search (dfs)

- sharad kumar.j aryansmit3754@gmail.com March 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dfs can be used to find the deepest node.

What is your algorithm, btw? Just saying dfs is pretty vague.

- T March 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can have recursive function

int length(Node *root)
{
if(root == 0)
return 0;
int right= length(root->right);
int left= length(root->left);
if( left > right )
return 1+ left;
else
return 1+right;

}

- Sumanth March 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can have recursive function

int length(Node *root)
{
if(root == 0)
return 0;
int right= length(root->right);
int left= length(root->left);
if( left > right )
return 1+ left;
else
return 1+right;

}

- Sumanth March 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

longest path != maximum depth.

- T March 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Longest Path= maximum path.

Can you please why explain why it is not true?

- Biker March 31, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Because maximum path length != maximum depth.

Consider a tree with root node 2, left child 1 and right child 3.

The longest path is 1-2-3, of length 3.

Maximum depth (with 2 as root) is 1.

The question did not state if it is a directed tree or undirected tree, but, since it is in the Trees&Graphs section and this is a well known problem :-), assume it is undirected.

- T March 31, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

maximum path != maximum depth assuming some root...

The question just says tree... it does not mention a root/directed edges etc.

- T March 31, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think longest path is same as maximum depth except for the difference that path information need to provided like {A C E H I } constitutes a longest path.

Longest Path : Set of nodes in order, which will have maximum number of nodes.
Maximum depth : Numerical value which tells about the height of the tree.

Please let me know if you don't agree.

- YetAnotherCoder April 06, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think longest path is same as maximum depth except for the difference that path information need to provided like {A C E H I } constitutes a longest path.

Longest Path : Set of nodes in order, which will have maximum number of nodes.
Maximum depth : Numerical value which tells about the height of the tree.

Please let me know if you don't agree.

- YetAnotherCoder April 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think longest path is same as maximum depth except for the difference that path information need to provided like {A C E H I } constitutes a longest path.

Longest Path : Set of nodes in order, which will have maximum number of nodes.
Maximum depth : Numerical value which tells about the height of the tree.

Please let me know if you don't agree.

- YetAnotherCoder April 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think longest path is same as maximum depth except for the difference that path information need to provided like {A C E H I } constitutes a longest path.

Longest Path : Set of nodes in order, which will have maximum number of nodes.
Maximum depth : Numerical value which tells about the height of the tree.

Please let me know if you don't agree.

- YetAnotherCoder April 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think longest path is same as maximum depth except for the difference that path information need to provided like {A C E H I } constitutes a longest path.

Longest Path : Set of nodes in order, which will have maximum number of nodes.
Maximum depth : Numerical value which tells about the height of the tree.

Please let me know if you don't agree.

- YetAnotherCoder April 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Try proving that they are the same and you will realize yourself.

- T April 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using a queue and not using recursion will be more clear.

- Anonymous April 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

tree_height(mynode *p)
{
if(p==NULL)return(-1);
h1=tree_height(p->left)+1;
h2=tree_height(p->right)+1;
return(max(h1,h2));
}


TESTED and WORKING

- T April 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Still Incorrect.

- The real T April 16, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How about using a wrapper function around the function tree_height(mynode*p) which calls it for all the nodes as root one by one. The maximum length for a tree would be the one which gives us maximum depth. Though this looks to be really inefficient if the number of nodes in the tree is large, but it is a correct solution.

- Observer April 19, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about using a wrapper function around the function tree_height(mynode*p) which calls it for all the nodes as root one by one. The maximum length for a tree would be the one which gives us maximum depth. Though this looks to be really inefficient if the number of nodes in the tree is large, but it is a correct solution.

- Anonymous April 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Path is different than height.

Assume that the tree data structure has an additional fields (int longestChildDepth). A node's longest path = sum of (node->left's, node->right's height, 1). The maximum of the sum is the longest path.

- Gautam May 13, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Path is different than height.

Assume that the tree data structure has an additional fields (int longestChildDepth). A node's longest path = sum of (node->left's, node->right's height, 1). The maximum of the sum is the longest path.

- Gautam May 13, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Path is different than height.

A node's longest path = sum of left childs height, right childs height and 1. The maximum of this number is the longest path.

- Gautam May 13, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This assumes it’s a binary tree and also assumes that the root has a degree greater than 1. For example it could be a completely unbalanced binary tree with the right sub-tree of every node empty. So you only have left sub-trees and left links. In this case the longest path is from the only leaf, the last node, to the root.

- Michael June 16, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming Longest Path of the tree is the diameter of the tree, the distance from the the root to its leaf.

Start from an arbitrary node and run a Bread First Search. Note the last node visited. From this node, run a BFS again. The distance from this node to the last node visited is the diameter of the tree.

- kunjaan May 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this seems to be the correct answer
as the deepest node from root may not be the farthest nodes in the tree.

- spur July 07, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The question, just mentioned a tree, is it right to assume that the tree is a binary tree and not an 'n'nary tree?

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

The question, just mentioned a tree, is it right to assume that the tree is a binary tree and not just an 'n'nary tree?

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

The question, just mentioned a tree, is it right to assume that the tree is a binary tree and not just an n-nary tree?

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

The question, just mentioned a tree, is it right to assume that the tree is a binary tree and not just an n-nary tree?

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

length of the longest path is called tree diameter.
diameter = max of ( height_of_left_child + 2 + height_of_right_child, diameter(left_child), diameter(right_child))

- dsun August 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is the same as tree diameter.The following link might be helpful

http://www.cs.duke.edu/~ola/courses/cps100spr96/tree/trees.html

- Prahlad August 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

answer = dia of tree, my code[working] in CPP is posted below.
here "depth" and "dia" are passed as reference to collect the values, they are un-initialized.

void BST::getDiameter(Node *head, int& depth, int& dia)const throw() {
	if (head == NULL) {
		//to counter the addition of 1 for a leaf node
		//depth is set to -1..........
     	depth = -1;
		dia = 0;
		return ;
	}

	int lDia;
	int rDia;
	int lDepth;
	int rDepth;

	getDiameter(head->left, lDepth, lDia);
	getDiameter(head->right, rDepth, rDia);

	depth = (lDepth > rDepth ? lDepth : rDepth) + 1;
	//           (A)
	//          /   \
	//         (C)   (B)
	//                \
	//                 (D)
	// totalDepth(A) = Depth(A->left) + Depth(A->right) +2 = 0+1 +2 = 3
	// IND = Inter Node Distance
	int IND = lDepth + rDepth + 2;
	dia = (lDia > rDia ? (lDia>IND?lDia:IND) : (rDia>IND?rDia:IND) );
	cout << "depth at " << head->data << " is:" << depth << " and dia is " << dia << endl;
 	
}

- googler September 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int Diameter(node *start)
{
    if(start == NULL) return -1;
    int l = 1 + Diameter(start->left);
    int r = 1 + Diameter(start->right);
    if(maximum < l+r+1)
       maximum = l+r+1;
    cout << maximum<< endl;
    return max(l,r);
}

the value of maximum will be longest path length which is also the diameter of tree.
maximum is a global variable here.

- Manish Agarwal October 04, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int Diameter(node *start)
{
    if(start == NULL) return -1;
    int l = 1 + Diameter(start->left);
    int r = 1 + Diameter(start->right);
    if(maximum < l+r+1)
       maximum = l+r+1;
    cout << maximum<< endl;
    return max(l,r);
}

the value of maximum will be longest path length which is also the diameter of tree.
maximum is a global variable here.

- Manish Agarwal October 04, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int Diameter(node *start)
{
    if(start == NULL) return -1;
    int l = 1 + Diameter(start->left);
    int r = 1 + Diameter(start->right);
    if(maximum < l+r+1)
       maximum = l+r+1;
    cout << maximum<< endl;
    return max(l,r);
}

the value of maximum will be longest path length which is also the diameter of tree.
maximum is a global variable here.

- Manish Agarwal October 04, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int depth(node *start)
{
if(start == NULL) return 0;
if(start->left == NULL)
int l = depth(start->left);
else
int l = 1 + depth(start->left);
if (start->right == NULL)
int r = depth(start->right);
else
int r = 1 + depth(start->right);
if(diameter< l+r)
diameter = l+r;
cout << diameter<< endl;
return max(l,r);
}

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

int depth(node *start)
{
if(start == NULL) return 0;
if(start->left == NULL)
int l = depth(start->left);
else
int l = 1 + depth(start->left);
if (start->right == NULL)
int r = depth(start->right);
else
int r = 1 + depth(start->right);
if(diameter< l+r)
diameter = l+r;
cout << diameter<< endl;
return max(l,r);
}

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

void findDiameter( node root,int& diameter,int& depth)
{
if (root->NULL)
{
depth = 0;
diameter = 0;
return 0;
}

findDiameter( root->left,diameterleft,depthleft);
findDiameter( root->right,diameterright,depthright);
diameter = max( depthleft+depthright+2, diameterleft,diameterright);
depth = max(depthleft+1,depthright+1);
}

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

void findDiameter( node root,int& diameter,int& depth)
{
if (root->NULL)
{
depth = 0;
diameter = 0;
return 0;
}

findDiameter( root->left,diameterleft,depthleft);
findDiameter( root->right,diameterright,depthright);
diameter = max( depthleft+depthright+2, diameterleft,diameterright);
depth = max(depthleft+1,depthright+1);
}

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

Too much shit, very little corn.

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

int Dia(struct node * tree)
{
if (tree == 0)
return 0;

int lheight = height(tree->left);

int leftD = Dia(tree->left);
int rightD = Dia(tree->right);

return max(lheight + rheight + 1, max(leftD, rightD));
}

- M August 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This looks pretty definitive to me:

h**p://crackinterviewtoday.wordpress.com/2010/03/11/diameter-of-a-binary-tree/

I saw this link somewhere on careercup, can't find it now though. One thing the above doesn't do is, find the actual path; just the length. I suppose it would be easy to modify it though, to associate the node responsible, with each length, in a pair (or tuple, or custom struct) and return /that/ instead of just the length. I haven't actually tried it though.

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

... here; question id: 1767700

(It's not /quite/ a dup because that's asking only for the diameter, not the complete path.)

However, though the overall algorithm is right, I'm dubious that it's correct in some details.

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

find the height of each child node and go to the child with the largest height. return path when a null node has been reached.

int getHeight(Node *root){
   if (root == NULL) return 0;
   if (root->left == NULL && root->right == NULL) return 1;
   else return max(getHeight(root->left), getHeight(root->right)) + 1;
}

int maxLevel(Node *root){
   int h = getHeight(root);
   int maxLevel = 0; int maxCount = 0;
   for (int i = 1; i<=h; i++){
  GNU nano 1.2.5                     File: spiralOrder.cpp

      int l = getHeight(root->left);
      int r = getHeight(root->right);
      if (l > r) return maxFuncUtil(root->left, path);
      else return maxFuncUtil(root->right, path);
   }
   else {
      return path;
      /*
      for (unsigned i = 0; i < path.size(); i++){
         cout << path[i] << "\n";
      */
   }
}

void maxPathFunc(Node *root){
   vector <int> path;
   vector <int> maxPath;
   maxPath = maxFuncUtil(root, path);
   for (unsigned i = 0; i < maxPath.size(); i++){
      cout << maxPath[i] << " -> ";
   }
   cout << "\n" << "length: " << maxPath.size() << "\n";
}

- AG April 27, 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