Amazon Interview Question for Software Engineer / Developers






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

int depth(node* root)
{
if(!root)
return -1;
return max(depth(root->left),depth(root->right))+1;
}

- winia September 17, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

For if(!root) case, why return -1 instead of 0?

- Kunzi August 02, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ Kunzi
Probably, because of the depth of the tree with only one node is considered to be euqual '0' (in accordance with the winia's algorithm).

- Aleksey.M January 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi AD. Can you please tell me what is wrong with my solution?

- Lakshmi September 26, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Lakshmi .
I guess too your solution is not right.
Consider a scenario ..

7
4 8
5 9
10
11

According to your code
here depth will be 3
But the real depth is 5.
Got it?

- Ratn October 12, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

yes..winia solution is right..thats the most efficient way.

- Bindu. September 27, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just find height of a tree

- Erik March 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ Lakshmi: Your solution is just a BS. I've never seen such shitty codes for such simple question.

The question is just to find the height of the tree and nothing else.
winia's solution is the standard way to doing it.

- Anonymous August 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

int max_deapth(node root){
int ld=0,rd=0,maxd=0;
node cur;

cur=root;

if(!root){
return maxd;
}

if(cur->left){

while(cur->left){
ld++;
cur=cur->left;
}
}

if(cur->right){
while(cur->right){
rd++;
cur=cur->right;
}
}

maxd = (rd > ld ? rd : ld);

return maxd+1;
}

- Lakshmi September 19, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I do not think your solution is correct. Solution by winia seems to be correct.

- AD September 20, 2008 | 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