Amazon Interview Question for Software Engineer in Tests






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

int finddepth(node* root)
{
if(root==null)
return 1;

depth=Max(finddepth(root->left),finddepth(root->right));
return depth;
}

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

bob you forgot to increment the depth as you travel down the tree

plus, I think the question was meant to ask the height of a tree. Depth is usually for some node in the tree.

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

int getHeight(struct treeNode *tree) {

if(tree == NULL) {
return 0;
}
leftHeight = getHeight(tree->left);
rightHeight = getHeight(tree->right);

if(leftHeight > rightHeight) {

return (leftHeight+1);
}
else {
return (rightHeight+1);
}
}

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

return depth(root->left)>depth(root->right)?
depth(root->left)+1:depth(root->right)+1

- anonymous March 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Question does not say binary tree... could it be a tree with >2 children?

- Walletless August 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If binary tree, its simple

int Height(Node *root)
{
if( !root ) return 0;

return 1 + max( Height(root->left), Height(root->right) );
}

int Max(int a, int b)
{
return (a > b)? a : b;
}

- Walletless August 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If binary tree, its simple

int Height(Node *root)
{
   if( !root ) 
      return 0;

   return 1 + max( Height(root->left), Height(root->right) );
}

int Max(int a, int b)
{
   return (a > b)? a : b;
}

- Walletless August 08, 2011 | Flag


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