Amazon Interview Question for Software Engineer / Developers






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

Using Depth First Search (DFS) with two parameters traverse(node, sum). When node = leaf check whether sum equal to given sum (SUM). Along the path whenever the sum exceeds SUM stop that branching.

The total running time is O(V+E) and space is O(V).

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

Using Depth First Search (DFS) with two parameters traverse(node, sum). When node = leaf check whether sum equal to given sum (SUM). Along the path whenever the sum exceeds SUM stop that branching.

The total running time is O(V+E) and space is O(V).

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

Pretty easy. DFS. BTW,it's not BST. so should not stop.

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

yes..shouldnt stop..subsequent nodes could have negative values

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

can somebody post the code please?

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

can someone please validate this:

f(node, accumulated_sum, test_val){
     if(node->left==null && node->right==null)
                return node->val+accumulated_sum==test_val

     return f(node->left, node->val+accumulated_sum, test_val) || f(node->right,accumulated_sum,test_val)
}

- aadith January 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

corrected version:

f(node, accumulated_sum, test_val){
     if(node->left==null && node->right==null)
                return node->val+accumulated_sum==test_val
     return f(node->left, node->val+accumulated_sum, test_val) || f(node->right,node->val+accumulated_sum,test_val)
}

- aadith January 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

above code look fine

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

bool HasPathSum(TreeNode root, int sum)
{
 return (NULL == root)?(0 == sum): ( (sum > root->info) && ( HasPathSum(root->left, sum-root->info) || HasPathSum(root->right, sum-root->info) ) );
}

- Ankush Bindlish January 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool HasPathSum(TreeNode root, int sum)
{
 return (NULL == root)?
  (0 == sum):
   ( (sum > root->info) &&
     (  HasPathSum(root->left, sum-root->info) ||
        HasPathSum(root->right, sum-root->info)
     )
   );
}

- Ankush Bindlish January 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ankush I believe you have to add the value at each node , you are just comparing the if "sum > value" at each node

- Rohit January 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use the characteristic that the IN-ODER TRAVERSAL result of a BST is ASCENDING. then it's straitforward........

- beyondfalcon January 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yea straitforward. except the crazy interview said it was a binary tree, not a bst. Perhaps we should put him in a strait jacket?

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

Hows that.. Lets say it is a BST.. how would an inorder traversal exactly help? The traversal would include left node , root and right node even at the leaf(last) level.. And for the sum we would want to do root and anyone of its child till we find a leaf.. Could you explain? Just curious..

- XeqtR January 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int tree(node ptr,int &num)
{static sum=0;
if(ptr==NULL)
{ if(sum==num)
{return 1;}
else
return 0;
}
sum+=ptr->val;
if(tree(ptr->left,num))
{return 1;}
else if(tree(ptr->right,num))
{return 1;}
else
{sum-=ptr->val;
return 0;}
}

- learner February 02, 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