Interview Question


Country: United States




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

The run time is of the order of

int printNextElement(Node* root, int num) {

  if(!root) {
    return INT_MAX;
  }

  int l = printNextElement(root->left, num);
  int r = printNextElement(root->right, num);

  int temp = INT_MAX;
  if(num < root->data)
    temp = std::min(l, root->data);
  else
    temp = l;


  return std::min(temp, r);
}

- Raghav April 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Question is on BST.

1. i believe, next smallest element would be rightmost of left element.
if right sub tree of left element is empty then it would be left node.

- amit April 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Inorder predecessor will be the answer

public int nextSmall(int val) {
tree = root;
while (tree != null) {
if (tree.value == val) {
node = tree;
break;
} else if (tree.value > val) {
parent = tree;
tree = tree.left;
} else {
parent = tree;
tree = tree.right;
}
}

if (node.left != null) {
if (node.left.right != null) {
return treeMax(node.left.right).value;
} else {
return node.left.value;
}
} else {
return parent.value;
}
}

- jindal.rohit018 April 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int getNextLeastElement(Node root, int n1, int tmp) {
int secondMax=-1,l,r,rootValue;

if(root !=null) {
rootValue=root.getValue();
l=getNextLeastElement(root.getLeft(), n1, tmp);
r=getNextLeastElement(root.getRight(), n1, tmp);
// System.out.println("node is : " + root.getValue() + secondMax);

if( l < r ) {
if(r <n1){
secondMax=r;
}
}
else{
if(l <n1 ){
secondMax=l;
}
}
if(secondMax < rootValue && rootValue <n1)
secondMax=rootValue;
}
return secondMax;
}

- ananam March 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

To get the next least element of a given node , we have to find the largest element in the left sub tree of a given node. Let me know if I am wrong.

- Avishek Gurung April 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private Node findNextLeastNode(Node root) 
{
	if(root == null) {
		return null;
	}

	if(root.right != null) {
		root = root.right;
		while(root.left != null) {
			root = root.left;
		}
	}

	return root;
}

- Sunil Dabburi April 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here... The function is called with the given node.

Node nextNode = findNextLeastNode(givenNode);
nextNode == null ? System.out.print("NULL") : System.out.print(nextNode.data);

- Sunil Dabburi April 28, 2014 | 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