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.
1
of 1 vote

How do we define a path? If we are assuming that the path in the tree starts from a root and can stop any where in the tree. A simple depth first search can solve this question.

void dfs(root,sum,path) {
if(!root) return;
if(sum ==0 ) { print path; return; }
if(sum < 0) { return; }
dfs(root->left,sum-root->value,path+","+root->value);
dfs(root->right,sum-root->value,path+","+root->value);
}

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

your code prints duplicate path.

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

A path may not start from root, right? So in that case we need to find a connected component in this tree (where only one of the outgoing links from a node is considered) whose values sum up to 'k'.
Please correct me if my understanding is wrong.

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

simple recursive algo: solu( subroot, target) = solution (subroot->left, target) union solution(subroot->right, target) uniton { solu(subroot->left, s1) union solu (subroot->right, s2) ,such that s1+s2 == target - subroot->value )

- jay February 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

with recursive dfs one could pass an array around:

checksum
    print all in array

    push(node);
    walk(node->left);
    walk(node->right);
    pop(node);

obviously this uses double the space needed, 1 for the actual stack and one for the passed around one. an alternative is to use stack based dfs, but theres a catch in doing it the convventional way (you cant pop the parent before going right).

- Anonymous March 25, 2012 | 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