Amazon Interview Question


Country: India




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

If we're dealing with a tree, just use a BFS and break on level k.

O(E+V)

- zbesst June 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why is this a tree?

- oOZz June 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@oOZz

I should have said graph, and assumed the problem was modeled as such. It was a bad assumption to say "tree" rather than "graph".

It could also be any other data structure where an element is referred to as a "node." The problem itself doesn't specify a data structure.

- zbesst June 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It would make an interesting problem if data structure was a binary tree with no parent pointers.

- Epic_coder June 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Of course BFS :)

- Anupam June 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>

struct BT
{
struct BT *l;
struct BT *r;
int data;
};
void findKthnodes(struct BT *root,int level,int k)
{

if(!root)
return;
if()
if(level==k)
printf("[%d]\t",root->data);

findKthnodes(root->l, level+1,k);
findKthnodes(root->r,level+1,k);

}

struct BT *nn(int d)
{
struct BT *root=(struct BT *)malloc(sizeof(struct BT) );
root->l=NULL;
root->r=NULL;
root->data=d;
return root;
}

main()
{
struct BT *rt=nn(5);
rt->l=nn(45);
rt->r=nn(565);
rt->l->l=nn(453);
rt->l->r=nn(33);
rt->r->l=nn(656);
rt->r->r=nn(55);
rt->r->l->r=nn(56);

int level=0;
int k=2;
findKthnodes(root,level,k);


}

- Anonymous June 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

its a generic question, might not be binary tree and that too may not be the root of a binary tree.

- praneeth June 18, 2013 | 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