Amazon Interview Question for Developer Program Engineers


Country: India
Interview Type: In-Person




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

nice and easy ))
here is the code:

min_so_far = +infty
// two node stacks holding the current path and
// the min sum path found so far
stack< node > cur_path, min_path;

void min_sum(node *t, int sum) {
    cur_path.push(t);
    sum += t->val;
    if(t->left != 0)
        min_sum(t->left, sum);
    else if(t->right != 0)
        min_sum(t->right, sum);
    else { // we are in the leaf node: check the sum
        if(min_so_far > sum) {
            min_so_far = sum;
            min_path = cur_path;
        }
    }
    cur_path.pop(t);    
}

call min_sum(root, 0);

- practice coding October 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if(min_so_far > sum) {
min_so_far = sum;
min_path = cur_path;
}

this code will execute when we reaches leaf node.
after this it will pop leaf node from the stack cur_path and then pop operation will keep on executing until stack gets empty.

bcozzz of if else{} condition it will not execute it right path.
this code will show left most path of the tree.

plzz tell me if i getting it wrong..

Thanks

- addi October 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

thanks for pointing out! it should be like this:

void min_sum(node *t, int sum) {
cur_path.push(t);
sum += t->val;
if(t->left == 0 && t->right == 0) { // found a leaf node
  if(min_so_far > sum) {
    min_so_far = sum;
    min_path = cur_path;
  }
}
if(t->left != 0) {
   min_sum(t->left, sum);
}
if(t->right != 0) {
   min_sum(t->right, sum);
}
cur_path.pop(t);
}

- Anonymous October 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

ok just see this every time decision involved weather u have to go left or right so just first check the minimum sum from t->left as root and t->right as a root which is less go there code for this algo
void PrintMinPath(struct node *T)
{
int ls,rs;
if(!T)
return;
printf("%d\n",T->info);
ls=GetMinSum(T->left);
rs=GetMinSum(T->right);
if(ls<=rs)
PrintMinPath(T->left);

else
PrintMinPath(T->right);
}

int GetMinSum(struct node *T)
{
int ls,rs;
if(!T)
return 0;
ls=GetMinSum(T->left)+T->info;
rs=GetMinSum(T->right)+T->info;
if(ls<=rs)
return ls;
else
return rs;
}

- geeks October 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice...ur code with some corrections

void PrintMinPath(struct node *T)
{
int ls=999,rs=999;
if(!T)
return;
printf("%d\n",T->info);
if(T->left) ls=GetMinSum(T->left);
if(T->right)rs=GetMinSum(T->right);
if(ls<=rs)
PrintMinPath(T->left);

else
PrintMinPath(T->right);
}

int GetMinSum(struct node *T)
{
int ls,rs;
if(!T)
return 0;
ls=GetMinSum(T->left)+T->info;
rs=GetMinSum(T->right)+T->info;
if(ls<=rs)
return ls;
else
return rs;
}

i guess now it runs perfectly

- kap October 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sure but you're calling GetMinSum potentially many times for each node. How about, 2 extensible arrays that are the depth of the tree (or stacks), and a globally least/greatest path sum, and one of the stacks for the best path so far.

Traverse the tree, keeping the sum-so-far on the way down at each node, and pushing the current node into the temporary stack, as you go (pop it on the way back up of course). At the leaf nodes, see whether the sum-so-far is greater / less than the global one, and if so copy the temporary stack to the global one.

At the end, print the global stack.

- jeremiah.bullfrog October 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C(a, b, k)
	for each i from 0 to k-1
		b[i] = a[i]
		
MP(n, s, k)
	if(n->l == NULL && n->r == NULL)
		if(s < sum)
			sum = s
			C(a, b, k)
		return
	a[k+1] = n->l->d	
	MP(n->l, s+n->l->d, k+1)	
	a[k+1] = n->r->d
	MP(n->r, s+n->r->d, k+1)

- Prateek Caire October 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int maxLen = 0;
node* targetLeaf;

list<char> crt, final;

void findMAXPath(node* root, int length)
{
	if(!root->lchild && !root->rchild)
	{
		if(length + root->element > maxLen)
		{
			targetLef = root;
			maxLen = length + root->element;
			final = crt;
			return;
		}
	}	
	if(!root->lchild)
	{
		crt.pushback('L');
		findMaxPath(root->lchild, length + root->elem);	
	}
	
	if(!root->rchild)
	{
		crt.pushback('R');
		findMaxPath(root->rchild, length + root->elem);
	}

	crt.popback();

	return;		
}

- qsgh February 22, 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