Amazon Interview Question for Software Engineer / Developers


Country: India




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

Your code is not returning result for all scenarios..
consider the case if only a single node tree say 10. It is BST but we are not returning anything so compiler would return an error i guess.

bool isBSTUtil(struct node* node, int min, int max) { 
	if (node==NULL) return false;

	if (node->data<min || node->data>max) return false;


	bool left = true;
	bool right = true;

	if(node->left)
		left = isBSTUtil(node->left, min, node->data); 
	
	if(node->right)
		right = isBSTUtil(node->right, node->data+1, max);

	return (left && right); 
}

- mr October 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if I do this in BStUtil
static int i=0;
if(root==null)
{
if(i==0) return false;
return true;
}
i++;
then it is correct ?

- Anonymous October 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

u can modify when calling recursively:

int isBSTUtil(struct node* node, int min, int max) { if (node==NULL) return(false);

if (node->data<min || node->data>max) return(false);


if(node->left)
isBSTUtil(node->left, min, node->data);
if(node->right)
return isBSTUtil(node->right, node->data+1, max);
}
Clasrufy if i made a mistake!

- sai25590 October 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What about creating default parameter with value false ? something like:

int isBSTUtil(struct node* node, int min, int max, bool defaultReturnValue = false);

int isBSTUtil(struct node* node, int min, int max, bool returnValue) { if (node==NULL) return(returnValue); 
// false if this node violates the min/max constraint if (node->data<min || node->data>max) return(false);
// otherwise check the subtrees recursively,
// tightening the min or max constraint
return
isBSTUtil(node->left, min, node->data,true) &&
isBSTUtil(node->right, node->data+1, max,true)
);

- rkjha October 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The easiest way I can think of is indicating if is checking the root or not in the first call.

int isBSTUtil(struct node* node, int min, int max, bool isRoot)
{
if (isRoot && node==NULL)
return false;
else
return true;
}

On the first call on isBST2, isRoot is true. On the recursive calls isRoot is always going to be false.

- rahvii October 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

forgot to add the NULL check on else:

int isBSTUtil(struct node* node, int min, int max, bool isRoot)
{
if (isRoot && node==NULL)
return false;
else if (node==NULL)
return true;
}

- rahvii October 20, 2012 | 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