Amazon Interview Question for Software Engineer / Developers






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

int dia(NODE* TREE, int *diameter)
{

if (tree == NULL)
return 0;

int left= 1+dia(tree->left,diameter);
int right = 1+dia(tree->left,diameter);

if ( (left+right) > *diameter )
*diameter= left+right;

if (left > right)
return left;
else
return right;


}

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

a level order traversal .. keeping track of the length of the queue and then the maximum length of the queue.... anything better than this?

- anonymous October 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

not correct

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

1. What kind of tree it is.
2. Please provide proper definition of Diameter of a tree.

- Question clarification October 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for each node, treat it as a subtree, record/compute its depth. ------ O(N)

traverse tree, for each node, find two children with largest depth, add depths up, plus 2. Always record the greater result during the traversal. ------ O(N)

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

well well well, can the question-poster please make use of his brain and "search" to make sure you are not creating duplicating posts. this question has been asked hell of times in this forum. please don't fool others by re-posting questions.
if you still find yourself sitting on the couch doing nothing , you can use this link

http://tinyurl.com/yj2rvdr

enjoy and good luck you.

- hitch-hiker October 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

let me know if iam wrong in thinking of diameter as something like this :
find out the height of the left subtree of the root
find out the height of the right subtree of the root ,

now add the heights + 1 should give me the diameter of the tree .. any suggestions or comments , abt this pls ..

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

main()
{
.....
.....
// diameter of the binary tree is :
j = height(root->left);
k = height(root->right);
diameter = j+k;
printf("the max diameter of the binary tree is %d",diameter);
getch();
}
int height(struct node *p)
{
int k = 1,l =1;
if(p==NULL)
return 0;
else
{
k = 1+height(p->left);
l = 1 + height(p->right);
if(k>l) return k;
else return l;
}
}

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

no you can not say like this as diameter is the largest distance b/w two nodes..
so conditions are
1) one node in left sub tree,one in right sub tree
2) both in left sub tree
3) both in right sub tree
can be done in O(N) in the following way...
(int, int) findDiameterInner(Node n)
{
if (n == null) return (0, 0)

(a, b) = findDiameterInner(n.leftChild);
(c, d) = findDiameterInner(n.rightChild);
depth = max(a, c) + 1;
dia = max(b, d, a + c);
return (depth, dia);
}

int findDiameter(Node root)
{
(depth, dia) = findDiameterInner(root);
return dia;
}

- gnu October 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you got the idea.

just a reminder, it didn't say it's a binary tree. So you need to search every child to get max depth & dia.

- geniusxsy October 19, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if u can come with tht on your own in the interview....u r pretty good!!!

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

gnu: What is the data structure for the types you defined in your code: (int, int), (a,b), (c,d), (depth, dia)? Are these simply arrays or maybe something else. Please clarify if possible. Thanks.

- newbie October 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int getDiameterCore(node *root, int *diameter)
{
	if (root == NULL)
		return -1;
	int h1 = getDiameterCore(root->left,diameter);
	int h2 = getDiameterCore(root->right,diameter);
	if (h1+h2+2 > *diameter)
		*diameter = h1+h2+2;
	return 1+max(h1,h2);
}

int getDiameter(node *root)
{
	int diameter = 0;
	getDiameterCore(root,&diameter);
	return diameter;
}

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

if height is a parameter of the node, then a O(height) = O(logN) can be achieved. here is the algorithm:

1. find the diameter at the node, as the sum of left->height + right->height + 1
2. follow the path from node to the node at max depth node.

mathematically, it can be argued that the max depth node at the root of the tree will remain the max node for every subtree, and thus will be a part of the diameter of the tree, whether or not the current node lies on the diameter.

- luvtheedragon February 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The following URL gives detailed explination:

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

- Anonymous March 11, 2010 | 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